-
object members
hey all,
i'd like to use objects of one class in other classes but i am still having trouble. this is what i have so far:
Code:
//-------------------------------------------------lineup.h--------------------
#ifndef lineup_h
#define lineup_h
#include "player.h"//Player is the other class
class Lineup
{
public:
Lineup();
void mf_Lineup_loadArray(Player *p_h1, Player *p_h2);
private:
Player home_lineup[2];//this is an array of pointers
//to objects of class Player
};
#endif
//-------------------------------------lineup.cpp--------------------------------
//Lineup.cpp
#include "lineup.h"
#include "player.h"
class Player;
Lineup::Lineup() //what's wrong with my constructor?
{}
void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
{
home_lineup[0]=&p_h1;
home_lineup[1]=&p_h2;
}
i get this error:
C:\mprog\sandlot\Lineup.cpp(9) : error C2512: 'Player' : no appropriate default constructor available
C:\mprog\sandlot\Lineup.cpp(13) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Player ** ' (or there is no acceptable conversion)
C:\mprog\sandlot\Lineup.cpp(14) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Player ** ' (or there is no acceptable conversion)
thanks in advance
-
Ok well you can either use the friend keyword (which would work for how you set up your code):
Class Lineup
{
friend class Player;
// You're code here
};
or you can make one derive from the other, which I don't think you want to do.
Then just remove that class Player statement from the cpp file which is I believe what is giving you the errors.
-
thanks for helping :)
i tried as you suggested but I'm getting this error instead:
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
Debug/sandlot.exe : fatal error LNK1120: 1 unresolved externals
here's my function call, am i doing it right:
Code:
Lineup l;
l.mf_Lineup_loadArray(h1, h2);//Player h1 and h2
here's the classes:
Code:
//-----------------------------------lineup.h---------------------------------
#ifndef lineup_h
#define lineup_h
#include "player.h"//Player is the other class
class Lineup
{
friend class Lineup;
public:
Lineup();
void mf_Lineup_loadArray(Player h1, Player h2);
private:
Player home_lineup[2];//this is an array of pointers
//to objects of class Player
};
#endif
//----------------------------------------Lineup.cpp---------------------------
#include "lineup.h"
#include "player.h"
#include <iostream.h>
void Lineup::mf_Lineup_loadArray(Player h1, Player h2)//this comes from main
{
home_lineup[0]=h1;
home_lineup[1]=h2;
cout<<"gotit";
}
-
What I see is this:
Code:
class Lineup
{
friend class Lineup;
//ect.
};
I believe this should be : friend class Player
-
ya, i'm sorry--typo. same problem though. here's the revised code:
Code:
//lineup.h
#ifndef lineup_h
#define lineup_h
#include "player.h"//Player is the other class
class Lineup
{
friend class Player;
public:
Lineup();
void mf_Lineup_loadArray(Player h1, Player h2);
private:
Player home_lineup[2];//this is an array of pointers
//to objects of class Player
};
#endif
am i making the function call right? christ, i just can't figure out what i'm doing wrong :(
-
Alright I believe you are declaring you're array wrong:
Player home_lineup[2];
should be: Player *home_lineup[2];
And then try this:
void mf_Lineup_loadArray(Player &h1, Player &h2);
And lastly:
Code:
void Lineup::mf_Lineup_loadArray(Player &h1, Player &h2)//this comes from main
{
home_lineup[0]=&h1;
home_lineup[1]=&h2;
cout<<"gotit";
}
-
i pluged in variations of * and & (i even tried passing the entire object). but still the same error:
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
Debug/sandlot.exe : fatal error LNK1120: 1 unresolved externals
thanks a lot for your help and i'm sorry to take up your time. if you still have any patience left here's all my code.
-
Ok, in you main.cpp file where you define your objects:
Code:
Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1), *p_h1=&h1;
Try doing this instead:
Code:
Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1); //define object
Player *p_h1; //create pointer of Player type
p_h1 = &h1; //assign address of h1 to p_h1
-
same error:
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
this is what i put in:
Code:
Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1);
Player *p_h1;
p_h1=&h1;
Player h2(14,1,1,1,1,1,1,1,"Boss",2);
Player *p_h2;
p_h2=&h2;
as far as i can tell, it should be the same difference. am i trying to do some weird here? what are my other options? i was able to compile everything that i wanted in procedural style code, but i wanted to learn something new and make my code look nice like everyone else's. ignoring the symantics, what design flaws are there? thanks again.
-
Well, I think we were looking at th wrong thing. The error calls for unresolved external symbol ...Lineup::Lineup() so you gave a prototype for a constructor but never defined it:
Code:
class Lineup
{
friend class Player;
public:
Lineup(); <- write here
void mf_Lineup_loadArray(Player &h1, Player &h2);
private:
Player *home_lineup[2];//this is an array of pointers
//to objects of class Player
};
So in Lineup.cpp add:
Code:
Lineup::Lineup()
{};
-
: plants a big wet kiss on traveller's cheek:
gfe2e
would you believe i messed around with that last night but not in conjunction the freind declaration. thanks a lot you made my week. :D
but . . . why is lineup.cpp not printing the cout?
-
this is funny. it won't print if i compile this:
Code:
void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
{
home_lineup[0]=p_h1;
home_lineup[1]=p_h2;
cout<<"testing";
}
but it will if i do something like this:
Code:
void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
{
home_lineup[0]=p_h1;
home_lineup[1]=p_h2;
cout<<"testing";
int b;
cin>>b;
}
-
I think its because you're clearing the screen right after you call the function( well after a 3 second wait anyway). That's why if you put a cin statement, you have time to see the message.
-
that's what i thought, so i deleted the clrscr()'s and still nothing. even when i scroll up through the console window to the beginning i still don't see it. but i do see the cursor blinky during the f_wait(). i'm stumped again.
-
wait, problem solved. i must of missed a clrscr(). . . .
gosh, what'll we talk about now if i don't come up with another idiotic question for you to help me with ;)
thanks a lot for your help, i won't be forgetting it. :D