Class - pointer - reference problem???

0 Earl Hudson · March 11, 2015

I have been trying to integrate 2 classes, learn pointers, and use multiple files...
I think for the most part it's going pretty well. However I have ran into a problem I can't seem to figure out. If I strip out the Item class, it works fine, if I put the item class back in I get the error in the Room class... 

The complete  code is here:
http://pastebin.com/u/E_Hudson

C:/Adventure/adventure/main.cpp:144: undefined reference to `Rooms::changeRoom(std::string)'
C:/Adventure/adventure/main.cpp:145: undefined reference to `Rooms::changeRoom(std::string)'
C:/Adventure/adventure/main.cpp:146: undefined reference to `Rooms::changeRoom(std::string)'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))

I also see in the debugger message collect2.exe error ??? What is that?

I'll take any advice you'd like to offer on the problem, or the over all code. Thanks guys, be brutal, I can take it. It's a good way to learn.

Post a Reply

Replies

Oldest  Newest  Rating
0 Earl Hudson · March 12, 2015
Weird... Okay

Item.cpp                     http://pastebin.com/KvkaxVWW   
Classes.cpp              http://pastebin.com/RpqHs538
Items.h                       http://pastebin.com/cDqVQ8tW
Classes.h                  http://pastebin.com/PnGahUQz
main.cpp                   http://pastebin.com/Ph586KwX
Roomdata.dat          http://pastebin.com/cCeikRYm
0 Earl Hudson · March 12, 2015
I use code blocks...  I get maybe about an hour a day to mess with this stuff. I'm not into all that, and try to teach myself as I go.
In any event, I found the error. it's fixed. I thank you Krootushas Gesu, and my friend @ http://webchat.freenode.net/? &channels=##coding ....

 
I replaced the code with an updated version. I have a new error. Now It says
error: expected constructor, destructor, or type conversion before '(' token
I'm trying to track it down now.
0 Earl Hudson · March 12, 2015
The code resides at the same locations. I just updated it.

Item.cpp http://pastebin.com/KvkaxVWW 
Classes.cpp http://pastebin.com/RpqHs538
Items.h http://pastebin.com/cDqVQ8tW
Classes.h http://pastebin.com/PnGahUQz
main.cpp http://pastebin.com/Ph586KwX
Roomdata.dat http://pastebin.com/cCeikRYm
Itemdata.dat http://pastebin.com/92yhHj1L

Have you tried using the command line/shell prompt/terminal to compile your code?
Honestly? I don't even know what that is. however, The settings for the extra command line arguments for batch builds in code blocks is     -na -nd -ns --batch-build-notify  (I hope that's what you are asking for)
everything else is set to auto detect or default.
The debugger is set to GDB/CDB debugger.

Please share the following:

Which command are you using to compile main.cpp?   
Code blocks  (F9)  Build and run

Which Google search keywords did you copy from the error message?
I searched google for C++  undefined reference to function

Which Google search results did you read thoroughly and try? 
All responses were stackoverflow,com. I read them all, and tried them all. It fixed the problem but another popped up and I searched them all until the problem vanished.

For each of those, what went wrong? 
As I said, another problem popped up ( expected constructor, destructor, or type conversion before '('  ) which when I fixed it,  turned into no constructor that matched. I fixed that, and now, it compiles and runs.

Did you try this one? What went wrong?
No sir, I did not try that one.

You said to "be brutal", but I'm not even being brutal -  I understand that. :)
Just blunt and to the point. - I appreciate that.
You said it's a good way to learn, right?  I improperly spoke... It's the best way to learn.
So do you want to learn, or not? Teach away...
0 Earl Hudson · March 16, 2015
Okay gents. Same problem. Same error. different line.

Code is located at :

main,cpp  ---  http://pastebin.com/Ph586KwX    (Problem in lines 150 - 151)
Classes.cpp ---  http://pastebin.com/RpqHs538
Classes.h ---  http://pastebin.com/PnGahUQz
Itemdata.dat ---  http://pastebin.com/vqWicAgs
Roomdata --- http://pastebin.com/cCeikRYm

Since I'm getting a linker error (Undefined reference to function)
I found this file on line.http://www.csee.umbc.edu/courses/undergraduate/202/fall04/Projects/CommonErrors.shtml which is a list of C++ compiler / linker errors. It says...

undefined reference
Example
/tmp/cc2Q0kRa.o: In function `main':
/tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)'
collect2: ld returned 1 exit status

Meaning 
Your code called the function Print, but the linker could not find the code for it in any .o file

Usual Causes
You forgot to link the .o file that contains the function
You misspelled the name of the function
You spelled the name of the function correctly, but the parameter list is different in someway

I've double checked all 4 causes and I don't see it... Any ideas?
0 Earl Hudson · March 16, 2015


class Item  // Standard Items
{

private:
    std::string name;
    std::string desc;
    int id;
    int weight;
    int loc;

public:

    Item(std::string name,std::string desc,int id,int weight,int loc);

    Item(std::string desc,int id,int weight,int loc);                                                         // <--- Right there.


    void set_Name(std::string iname);
    std::string get_Name();
    void set_Desc(std::string idesc);
    std::string get_Desc();
    void set_Id(int iid);
    int get_Id();
    void set_Loc(int iloc);
    int get_Loc();
    void set_Weight(int iweight);
    int get_Weight();
    // more methods to follow
};

0 Earl Hudson · March 16, 2015
/images/forum/upload/2015-03-16/8fca40fa270c0edf3b424ea9fc3cbb53.jpg
0 Earl Hudson · March 16, 2015
Never mind, I see it. I need to implement it rather than just declare it. Okay... Bonehead strikes again! :)
0 Earl Hudson · March 17, 2015
Hey, I just learned to write basic classes, and with my project it doesn't APPEAR to need anything complicated... I'm learning. :)
0 Earl Hudson · March 13, 2015
DOS... lol, I grew up on Dos. Okay, I figured out how to install the feature (from Codeblocks) into the path.
Executed it, and I had no problems.  Okay, what's next boss?

/images/forum/upload/2015-03-13/3519e0eb4be6c9f7cc286673720ef12d.jpg
  • 1

C++

107,113 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator