I figured out my other problem, but...

This is a discussion on I figured out my other problem, but... within the C++ Programming forums, part of the General Programming Boards category; I am still having some issues with this whole class thing in Visual C++ 6.0. Based on the code I ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    I figured out my other problem, but...

    I am still having some issues with this whole class thing in Visual C++ 6.0. Based on the code I am pasting below, is it best to create the app as a Win 32 Console App or just a Win 32 App? Also, I must be doing something wrong because the nature of the error messages I am getting lead me to believe that I am sourcing in my class (for lack of a better term) correctly. Any help that anyone could provide would be awesome... I'll paste the code and errors.

    Main source code:
    Code:
    #include <iostream>
    #include <string>
    #include "square.h"
    #include "ezwin.h"
    
    using namespace std;
    
    
    int ApiMain() {
    	SimpleWindow Test;
       	Test.Open();
    	Square GreenSquare(Test);
    	GreenSquare.SetColor(Green);
    	GreenSquare.SetPosition(3.5,2.5);
    	GreenSquare.SetSize(1.5);
    	
    	GreenSquare.Draw();
        cout << "Type a character followed by a\n" << "return to remove the display and exit" << endl;
    	char AnyChar;
    	cin >> AnyChar;
    	Test.Close();
    	return 0;
    }
    square.h (header file/class):
    Code:
    #ifndef SQUARE_H
    #define SQUARE_H
    #include "ezwin.h"
    
    class Square {
    	public:
    		Square(SimpleWindow &W);
    		void Draw();
    		void SetColor(const color &Color);
    		void SetPosition(float XCoord, float YCoord);
    		void SetSize(float Length);
    	private:
    		color Color;
    		float XCenter;
    		float YCenter;
    		float SideLength;
    		SimpleWindow &Window;
    };
    #endif
    Here are the errors, I created this app as a plain old Win 32 Application (totally blank):

    test0422.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(12) : error C2065: 'Square' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(12) : error C2146: syntax error : missing ';' before identifier 'GreenSquare'
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(12) : error C2065: 'GreenSquare' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(13) : error C2228: left of '.SetColor' must have class/struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(14) : error C2228: left of '.SetPosition' must have class/struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(15) : error C2228: left of '.SetSize' must have class/struct/union type
    C:\Program Files\Microsoft Visual Studio\MyProjects\pleasework\test0422.cpp(17) : error C2228: left of '.Draw' must have class/struct/union type
    Error executing cl.exe.

    pleasework.exe - 7 error(s), 0 warning(s)


    Thanks!

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    this might be a dumb question but, is Square.h in the right directory?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    There's no such thing as dumb questions, just dumb noobs like myself. :P

    I did double check and square.h is in the same folder as my .cpp file (my main source code)

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Is there a square.cpp file defining the fxns.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    That there is not.. (a square.cpp file)... what should typically go into the .cpp file that I am lacking in the .h file? Forgive me if my questions seem a bit dense.. the book I am reading isn't real clear on this... thanks..

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Also, does it matter in this case which application I choose to make this (either Win 32 App or Win 32 Console App)? I have tried it both ways and it seems like I get less errors trying it as a Win 32 App, so I have to figure that's the right way.... thanks again

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is the header file called SQUARE.H, Square.h, or square.h?

    "Based on the code I am pasting below, is it best to create the app as a Win 32 Console App or just a Win 32 App?"

    I don't know, but I think you should probably figure that out. I use Win32 Console App for most of my practice code, and Win32 app for a Windows program when not using MFC. My book says if you try to create a Windows program by creating a Win32 Console App, you're going to get errors, so maybe if you do it the other way around, like you seem to be doing, you'll get errors too.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    The header file is actually called square.h

    It sounds like I may want to do this as a console instead then? It seems like I had more problems with errors going that way, but if that's the right way I'll try it...

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    the .cpp file will contain the function definitions.

    ex

    Code:
    // my .h file
    
    class thing
    {
         int function(int, float);
    
    };
    
    
    // my .cpp file
    
    int thing::function(int i, float f)
    {
        // do stuff here
       return something;
    }

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To do it as console use:

    int main()

    not

    int ApiMain()

    as the entry point.

    The cpp file should contain function definitions for the methods of the Square class, whether you do this in console or not.

    //////////////////////
    //Square.cpp
    /////////////////////
    #include "Square.h"

    Square(SimpleWindow &W);
    {}
    void Draw()
    {}
    void SetColor(const color &Color)
    {}
    void SetPosition(float XCoord, float YCoord)
    {}
    void SetSize(float Length)
    {}

    Just complete each function body within each {} as desired

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "That there is not.. (a square.cpp file)... what should typically go into the .cpp file that I am lacking in the .h file?"

    The functions declared in the .h file, have to be defined somewhere or how else is the program going to know what to do when a function is called?

    It sounds like you're in way over your head with this program. I suggest you practice some simple Hello World type Win32 Console App programs first with everything in one file, and then learn how to create programs with multiple files. It appears classes are way down the road for you. It's better to start at the beginning and work your way through C++, than jump in towards the end and try to figure out what's going on.

    "Just complete each function body within each {} as desired"

    I seriously doubt the poster has any idea what you're talking about.

    I don't have much experience with windows programming, but the poster's code looks like part of a win32 program to me.
    Last edited by 7stud; 04-22-2003 at 09:37 PM.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Well, I guess I have to go back to the drawing board on this one... I have done quite a few simpler programs leading up to this one and haven't had any issues. I am sure that whatever I am doing wrong is right in front of my face and I just can't quite seem to get it yet. I will keep trying though!

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I have done quite a few simpler programs leading up to this one and haven't had any issues. "

    Ok, I thought this might be the first program you've played around with. Did any of your previous programs involve functions? What about programs with multiple files? What type of projects were you creating for those?

    Besides the type of project issues you're having, in your current program you have these functions declared in your class:

    Square(SimpleWindow &W);
    void Draw();
    void SetColor(const color &Color);
    void SetPosition(float XCoord, float YCoord);
    void SetSize(float Length);

    but where are those functions defined? Here is a simple function declaration:

    void Double_and_Print(int a);

    Here is the definition for that function:
    Code:
    void Double_and_Print(int a)
    {
          cout<<2*a<<endl;
    }
    Last edited by 7stud; 04-22-2003 at 10:21 PM.

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    The C++ related stuff that I have done prior to this was pretty basic... programs that prompted for user input and output responses based on the input.. or programs that took user input and made a calculation of some sort and then output the answer.. nothing too major.. I have done some VB in the past (though some argue whether that's a real language LOL) and I've also done some regular C programming as well as Korn Shell Scripting in UNIX. I haven't actually done a whole heckuva lot in terms of functions in C++ yet though, but I have dealt with functions of course quite often in the other languages, so I have to figure it to be similar...

    I think the declaring of the functions must be my missing piece. The problem in the book says to take the snippet of code which is basically the class Square portion and use that to create a program that draws a square with parameters defined in the problem... I don't know if I am making sense....

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    It's probably an exercise started for you. But you need to define the fxns.

    What book is this? You might be missing something..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21