Jump to content
  • Advertisement
Sign in to follow this  
Menace2Society

C++ is so frustrating [noob question]

This topic is 3924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay so I'm trying to transition from C# to C++, right now I'm using Sams Teach Yourself C++ in 21 Days. It really pains me to no end that even simple things like adding a class header causes random errors that a noob like me has no way of solving. So I create a class header file called "Cat.h", declare all its public and private members. Then I create a seperate file called Cat.cpp and implement all the methods declared in Cat.h. Then I #include "Cat.h" like the book tells me to, try to create a Cat object in Main, and guess what happens? Compiler error: " 'Cat' Undeclared identifier ". First of all, what does this even mean, and how do you fix it? I tried doing all sorts of header linking combinations to no avail, and it sucks that I've gotta waste you guys' time with n00b questions, but what am I doing wrong and more importantly what am I supposed to be doing? This is my Main file (well, all the parts that matter in this context) #include "Cat.h" #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { Cat yourCat(catSize, catWeight); } My header file "Cat.h"
Quote:
#include <iostream> class Cat { public: Cat(int size, int weight); void Eat(); void Exercise(); int GetSize() const; int GetWeight() const; private: int weight; int size; }
"Cat.cpp" (This part is just loaded with errors such as "left of .size needs class/struct/union" or "Overloaded function is not found in Cat."
Quote:
#include "stdafx.h" #include "Cat.h" Cat::Cat(int size, int weight) { this.size = size; this.weight = weight; } void Cat::Eat() { std::cout << "You are feeding the cat, oh no it's getting fatter!"; weight++; } void Cat::Exercise() { std::cout << "You're making your fatass cat exercise, thank god. It's losing weight!"; weight--; } int Cat::GetSize() { return this.size; } int Cat::GetWeight() { return this.weight; //Error }
Everything I did here was in accordance with what the book tells me I'm allowed to do, how does VC++ handle things?

Share this post


Link to post
Share on other sites
Advertisement
Just after a quick glance, in Cat.cpp, when you use the 'this' pointer, 'this' is a pointer, meaning you need to use the -> and not the . ie this->someField = someValue;

Share this post


Link to post
Share on other sites
Quote:
Original post by Menace2Society
Sams Teach Yourself C++ in 21 Days.

SAMS produces nothing but crap. I hope you kept your receipt and can return the book, if not... well, winter is coming, I guess you could always use it in your fireplace?

'Cat' Undeclared identifier

This means the compiler doesn't know what the heck you're talking about when you mention Cats, or has been confused by earlier errors. The compiler needs to see the definition of the class ("class Cat { ... };"), either by having it in each file using Cat before that point, or by having a file containing it #included before that point.

Other errors:
int _tmain(int argc, _TCHAR* argv[])
{
Cat yourCat(catSize, catWeight); // neither of these variables have been defined, try plugging in 42 for each instead
}



Cat::Cat(int size, int weight)
{
this.size = size; // "this" is a pointer, not a reference or direct variable -- you want "this->size" instead
//...or, better yet, and "initializer list" (googleable term for you there), which no author of a Sams book will probably have even heard of.
...

Share this post


Link to post
Share on other sites
Original post by Menace2Society
... Compiler error: " 'Cat' Undeclared identifier ". First of all, what does this even mean, and how do you fix it?...
    #include "Cat.h"
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
Cat yourCat(catSize, catWeight);
}
The error means that the symbol Cat has not been declared yet, so the compiler doesn't know what it is. Your problem is the result of a common mistake. All text before the #include "stdafx.h" is ignored by the compiler. #include "stdafx.h" must be the first #include (usually).

Share this post


Link to post
Share on other sites
Well now there's a few less errors, but the program still doesn't work. Here's the entire list of errors:

error C2533: 'Cat::{ctor}' : constructors not allowed a return type
'int Cat::GetSize(void)' : overloaded member function not found in 'Cat'
error C2146: syntax error : missing ';' before identifier 'yourCat'
error C2065: 'Cat' : undeclared identifier

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Menace2Society
Sams Teach Yourself C++ in 21 Days.

SAMS produces nothing but crap. I hope you kept your receipt and can return the book, if not... well, winter is coming, I guess you could always use it in your fireplace?


Here is a list of books with some reviews so that you wont make the same mistake again :)
http://www.gamedev.net/columns/books/books.asp?CategoryID=21

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
Quote:
Original post by Menace2Society
Sams Teach Yourself C++ in 21 Days.

SAMS produces nothing but crap. I hope you kept your receipt and can return the book, if not... well, winter is coming, I guess you could always use it in your fireplace?

'Cat' Undeclared identifier

This means the compiler doesn't know what the heck you're talking about when you mention Cats, or has been confused by earlier errors. The compiler needs to see the definition of the class ("class Cat { ... };"), either by having it in each file using Cat before that point, or by having a file containing it #included before that point.

Other errors:
int _tmain(int argc, _TCHAR* argv[])
{
Cat yourCat(catSize, catWeight); // neither of these variables have been defined, try plugging in 42 for each instead
}



Cat::Cat(int size, int weight)
{
this.size = size; // "this" is a pointer, not a reference or direct variable -- you want "this->size" instead
//...or, better yet, and "initializer list" (googleable term for you there), which no author of a Sams book will probably have even heard of.
...



Well the uninitialized parameters are actually initialized, I just chose to not include them in the quote (my mistake).

Dancin - I also put #include "cat.h" after "stdafx.h" and it still produces the same errors

Share this post


Link to post
Share on other sites
class Cat
{
public:
Cat(int size, int weight);
void Eat();
void Exercise();
int GetSize() const;
int GetWeight() const;

private:
int weight;
int size;
}


Note lacking semicolon after the class }

class cat {
.....
};

That should take care of any 'constructor return type' errors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!