Sign in to follow this  
Devil0150

visual c++ error messages

Recommended Posts

I get some error messages when I try to compile my code. I have two classes: class myClass { ... }; class otherClass { ... myClass* somePtr; }; This is one of the error messages I get when I try to compile. error C2143: missing ';' before '*' This error happens at the line where I declare the myClass pointer. And the declaration in the line before that, DOES have a ';' in the end. Any help how I can fix this. (I have all my code in one file).

Share this post


Link to post
Share on other sites
constuctors should be public, try changing it

class myClass
{
public:
myClass();
std::string name;
short first, second, third, fourth, fifth;
short pp;
};

also if you define a function of a class outside of a class you should use this


myClass::myClass()
{
}

Share this post


Link to post
Share on other sites
I guess you're not showing us all of the code. Look at the file being compiled when the error was show (The output will show the name of the .cpp file); work your way up the includes to where otherClass is and see if myClass was declared at any point before it. I'm guessing that it isn't.

Share this post


Link to post
Share on other sites
The problem is that you've got those two class defined in separate files, right? In that case you need to forward-declare myClass before otherClass, like so:
// file 1
class myClass
{
...
};

// file 2
class myClass;

class otherClass
{
myClass *somePtr;
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Devil0150
(I have all my code in one file).


thats a bad habbit to get into
but for now it would eliminate the need to forward declare your class.. hmm is that all of your code?

edit:: btw if no one replies to your accident thread, you can edit and delete your post.

also, this is working code so check what you have on yours compared.

class MyClass
{
int one, two, three;

public:
MyClass()
{

}
};

class ClassTwo
{
int one, two;
MyClass* ptr;

public:
ClassTwo()
{

}
};

Share this post


Link to post
Share on other sites


class myClass
{
public:
myClass();
std::string name;
short damage, defense, spDamage, spDefense, speed;
short pp;
};

myClass::myClass()
{
}

class otherClass
{
public:
otherClass();
void Attack();
void Run();
bool isWild;
string name;
short number;
string type1;
string type2;
otherClass* Evolution;
myClass* myATKs; /*this is where I get the error*/
myClass* chosenATK;
private:
short atkLVL[];
short chooseATK();
void ListATK();
};


This is my code in the two classes. I dont know if this is that much important but I get two other errors on the same line:
error C4430: missing type specifier - int assumed. Note: C++ does not support default int

Share this post


Link to post
Share on other sites
Is that all in the same .cpp file, in that order?

If not, then you've omitted the cause of the problem.

If so, it should work - as long as you have #include'd <string>, anyway. Try a clean rebuild.

Share this post


Link to post
Share on other sites
This is how all my code looks like.


#include <iostream>
#include <cstdlib>
#include <string>
using std::cout;
using std::cin;
using std::string;

class someClass;
class myClass;
class otherClass;

class someClass
{
...
// there are no objects of other classes here.
};

class myClass
{
public:
myClass();

std::string name;

short damage, defense, spDamage, spDefense, speed;

short pp;
};

class otherClass

{

public:

otherClass();

void Attack();

void Run();

bool isWild;

string name;

short number;

string type1;

string type2;

otherClass* Evolution;

myClass* myATKs; /*this is where I get the error*/

myClass* chosenATK;

private:

short atkLVL[];

short chooseATK();

void ListATK();

};

int main()
{
return 0;
}


This is all my code for now. So, what am I doing wrong.

Share this post


Link to post
Share on other sites
When I compile the code the only error I get is from the '...' and I'm going to assume that isn't really in your code. I get no errors and one warning (you used a zero-sized array for atkLVL[]).

Share this post


Link to post
Share on other sites
Hello ... I don't know if I got something here but I don't think you can have otherClass* Evolution; in the declaration of otherClass because it is the same class. I don't know if I am right but I think it's illegal to have a pointer of the same class you are defining as member variable.



Share this post


Link to post
Share on other sites
Quote:
Original post by ArthY303
Hello ... I don't know if I got something here but I don't think you can have otherClass* Evolution; in the declaration of otherClass because it is the same class. I don't know if I am right but I think it's illegal to have a pointer of the same class you are defining as member variable.
No, that's fine (for example, how could you implement a linked list without being able to have pointers to the same class?)

The code as posted is still obviously not the complete code... It compiles fine for me.

Share this post


Link to post
Share on other sites
This is all the code(I removed the comments cause they're not in english).I'm trying to make some kind of Pokemon text based game.


#include <iostream>
#include <cstdlib>
#include <string>
using std::cout;
using std::cin;
using std::string;

class Attack;
class Player;
class Pokemon;

class Player
{
public:
Player();
string name;
char sex;
short age;
private:
void SetName();
void SetSex();
void SetAge();
};

class Attack
{
public:
Attack();
std::string name;
short damage, defense, spDamage, spDefense, speed;
short pp;
};

class Pokemon
{
public:
Pokemon();
void Attack();
void Run();
bool isWild;
string name;
short number;
string type1;
string type2;
Pokemon* Evolution;
Attack* myATKs;
Attack* chosenATK;
private:
short atkLVL[];
short chooseATK();
void ListATK();
};

Player::Player()
{
SetName();
SetSex();
SetAge();
}

void Player::SetName()
{
cout << "\nWhats your name? ";
cin >> name;
}

void Player::SetSex()
{
cout << "\nAre you male or female?(m/f) ";
cin >> sex;
}

void Player::SetAge()
{
cout << "\nYour age: ";
cin >> age;
}

Attack::Attack()
{
}

int main()
{
return 0;
}

Share this post


Link to post
Share on other sites
Mmh, somewhat of awild guess here, but does the member function Attack by any chance conflict with the class with the same name? That might explain why your previous example does compile (according to Codeka).

Just as a test: when you move the two pointers up, so that they're declared above the member function, does it still yield the same error?

class Pokemon
{
public:

Pokemon();

Attack* myATKs; // moved
Attack* chosenATK; // moved

void Attack();
void Run();
bool isWild;
string name;
short number;
string type1;
string type2;
Pokemon* Evolution;

private:

short atkLVL[];
short chooseATK();
void ListATK();
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this