[RESOLVED] C++ class help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] C++ class help!

  1. #1
    Join Date
    Jun 2012
    Posts
    11

    [RESOLVED] C++ class help!

    *SOLVED
    I'm having trouble deciphering the error codes that I'm getting and fixing whats going on. Help would be much appreciated!
    Pen.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Pen.h"
    
    using namespace std;
    int main()
    {
    	Pen FavoritePen;
    	FavoritePen.InkColor = blue;
    	FavoritePen.ShellColor = clear;
    	FavoritePen.CapColor = black;
    	FavoritePen.Style = ballpoint;
    	FavoritePen.Length = 6.0;
    	FavoritePen.Brand = "Pilot";
    	FavoritePen.InkLevelPercent = 90;
    	Pen WorstPen;
    	WorstPen.InkColor = blue;
    	WorstPen.ShellColor = red;
    	WorstPen.CapColor = black;
    	WorstPen.Style = felt_tip;
    	WorstPen.Length = 3.5;
    	WorstPen.Brand = "Acme Special";
    	WorstPen.InkLevelPercent = 100;
    	cout << "This is my favorite pen" << endl;
    	cout << "Color: " << FavoritePen.InkColor << endl;
    	cout << "Brand: " << FavoritePen.Brand << endl;
    	cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
    	system("pause");
    	return 0;
    }
    my header file Pen.h
    Code:
    #include <iostream>
    #include <string>
    #ifndef PEN_H_INCLUDED
    #define PEN_H_INCLUDED
    using namespace std;
    enum Color
    {
    	blue,
    	red,
    	black,
    	clear
    };
    enum PenStyle
    {
    	ballpoint,
    	felt_tip,
    	fountain_pen
    };
    class Pen
    {
    public:
    	Color InkColor;
    	Color ShellColor;
    	Color CapColor;
    	PenStyle Style;
    	float Length;
    	string Brand;
    	int InkLevelPercent;
    	void write_on_paper(string words)
    	{
    		if (InkLevelPercent <= 0)
    		{
    			cout << "Oops! Out of ink!" << endl;
    		}
    		else
    		{
    			cout << words << endl;
    			InkLevelPercent = InkLevelPercent - words.length();
    		}
    	}
    	void break_in_half()
    	{
    		InkLevelPercent = InkLevelPercent / 2;
    		Length = Length / 2.0;
    	}
    	void run_out_of_ink()
    	{
    		InkLevelPercent = 0;
    	}
    };
    #endif // PEN_H_INCLUDED
    and my error codes..

    1>------ Build started: Project: Pen, Configuration: Debug Win32 ------
    1> Pen.cpp
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(11): error C2065: 'Pen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(11): error C2146: syntax error : missing ';' before identifier 'FavoritePen'
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(11): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(12): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(12): error C2228: left of '.InkColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(12): error C2065: 'blue' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(13): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(13): error C2228: left of '.ShellColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(13): error C2065: 'clear' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(14): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(14): error C2228: left of '.CapColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(14): error C2065: 'black' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(15): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(15): error C2228: left of '.Style' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(15): error C2065: 'ballpoint' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(16): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(16): error C2228: left of '.Length' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(17): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(17): error C2228: left of '.Brand' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(18): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(18): error C2228: left of '.InkLevelPercent' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(19): error C2065: 'Pen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(19): error C2146: syntax error : missing ';' before identifier 'WorstPen'
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(19): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(20): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(20): error C2228: left of '.InkColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(20): error C2065: 'blue' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(21): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(21): error C2228: left of '.ShellColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(21): error C2065: 'red' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(22): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(22): error C2228: left of '.CapColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(22): error C2065: 'black' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(23): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(23): error C2228: left of '.Style' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(23): error C2065: 'felt_tip' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(24): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(24): error C2228: left of '.Length' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(25): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(25): error C2228: left of '.Brand' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(26): error C2065: 'WorstPen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(26): error C2228: left of '.InkLevelPercent' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(28): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(28): error C2228: left of '.InkColor' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(29): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(29): error C2228: left of '.Brand' must have class/struct/union
    1> type is ''unknown-type''
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(30): error C2065: 'FavoritePen' : undeclared identifier
    1>c:\users\jonbecher\documents\visual studio 2012\projects\pen\pen\pen.cpp(30): error C2228: left of '.InkLevelPercent' must have class/struct/union
    1> type is ''unknown-type''
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by JonBMN; August 7th, 2012 at 04:01 PM. Reason: SOLVED

  2. #2
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: C++ class help!

    ( copy all you code into notepad and save for later use)this ever easy to solve , create a new class called Pen through visual studio class wizard , do not use inline functions use given cpp file for function definition and relevant header deceleration , call all of this from the main.cpp which has the main function.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center