Source code from book not working!!!

Started by
10 comments, last by enderfults 16 years, 1 month ago
For those new to the thread, please see my reply before viewing the source code below for the sake of an updated problem I need to start reading my book all over again because i'm so confused and lost now because this has been eating at me for a while - how does source code from a book not work??? I'll provide the code and errors below. It either won't compile, or won't link: (Using Visual Studio .NET 2008 btw)

//classes.h
#include <string>
#include <iostream>

using namespace std;
class Bow
{
     //data member declarations
     string color;
     bool drawn;
     int numOfArrows;
public:     
     Bow(string aColor);   //constructor
     ~Bow();                    //destructor
     
     //methods
     void draw();
     int fire();
};

------------------------------------------------------

//classes.cpp
#include "classes.h"
#include <iostream>
#include <ctime>
using namespace std;

Bow::Bow(string aColor)
{
     numOfArrows = 10;
     drawn = false;
     color = aColor;
     srand((unsigned)time(0)); //seeds the time (we need the rand() function in the fire() method)
}

Bow::~Bow()
{
}

//draws the bow
void Bow::draw()
{
     drawn = true;
     cout<< "The "<<color<<" bow has been drawn." <<endl;
}
//fires the bow if drawn
int Bow::fire()
{
     if(!drawn)
     {
          cout<< color << " has not been drawn and therefore could not fire." << endl;
          return 0;
     }
     int score;
     score = rand() % (10 - 0 + 1) + 0;
     if(score == 0)
          cout<<color<< " missed the target!!!" <<endl;
     else
          cout<< color << " scored " << score << " points!!!" <<endl;
     return score;
}

-----------------------------------------------

//test.cpp
#include <iostream>
#include <string>
#include <ctime>
using namespace std; 
     
int main( void );
void bowTest(void);

class Bow
{
     //data member declarations
     string color;
     bool drawn;
     int numOfArrows;
public:     
     Bow(string aColor);   //constructor
     ~Bow();                    //destructor
     
     //methods
     void draw();
     int fire();
};


Bow::Bow(string aColor)
{
     numOfArrows = 10;
     drawn = false;
     color = aColor;
     srand((unsigned)time(0)); //seeds the time (we need the rand() function in the fire() method)
}

Bow::~Bow()
{
}

//draws the bow
void Bow::draw()
{
     drawn = true;
	 cout<< "The "<<color<<" bow has been drawn." <<endl;
}
//fires the bow if drawn
int Bow::fire()
{
     if(!drawn)
     {
          cout<< color << " has not been drawn and therefore could not fire." << endl;
          return 0;
     }
     int score;
     score = rand() % (10 - 0 + 1) + 0;
     if(score == 0)
		 cout << color<< " missed the target!!!" <<endl;
     else
          cout << color << " scored " << score << " points!!!" <<endl;
     return score;
}


//the main function
int main( void )
{
     bowTest();
     return 0;
}

//tests the bow class
void bowTest(void)
{
     cout<<"yellow bow created"<<endl;
     Bow yellow("yellow");
     cout<<"attempting to fire yellow bow"<<endl;
     yellow.fire();
     cout<<"drawing the bow"<<endl;
     yellow.draw();
     cout<<"attempting to fire yellow bow"<<endl;
     yellow.fire();
}


----------------------------------------------------- The above uses <string> in test.cpp and generates the following errors: 1>------ Build started: Project: classes, Configuration: Debug Win32 ------ 1>Compiling... 1>test.cpp 1>Linking... 1>test.obj : error LNK2005: "public: __thiscall Bow::Bow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Bow@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) already defined in classes.obj 1>test.obj : error LNK2005: "public: __thiscall Bow::~Bow(void)" (??1Bow@@QAE@XZ) already defined in classes.obj 1>test.obj : error LNK2005: "public: void __thiscall Bow::draw(void)" (?draw@Bow@@QAEXXZ) already defined in classes.obj 1>test.obj : error LNK2005: "public: int __thiscall Bow::fire(void)" (?fire@Bow@@QAEHXZ) already defined in classes.obj 1>C:\Users\Korsen\Documents\Visual Studio 2008\Projects\classes\Debug\classes.exe : fatal error LNK1169: one or more multiply defined symbols found 1>Build log was saved at "file://c:\Users\Korsen\Documents\Visual Studio 2008\Projects\classes\classes\Debug\BuildLog.htm" 1>classes - 5 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== Using the above without <string> which is the EXACT CODE from the book, generates the following (repeat the same code 4x for different lines in which the same cout << color << etc is used: (Erased to just the errors for the sake of this post) 1>------ Build started: Project: classes, Configuration: Debug Win32 ------ 1>Compiling... 1>test.cpp 1>c:\users\korsen\documents\visual studio 2008\projects\classes\classes\test.cpp(41) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) 1>Build log was saved at "file://c:\Users\Korsen\Documents\Visual Studio 2008\Projects\classes\classes\Debug\BuildLog.htm" 1>classes - 4 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== Please help. I'm trying to learn how to build my own engine, or at least learn how to manipulate one to create my own game. Thanks in advance. EDIT: Reading this code, it looks like it should be two different programs... am I just missing something here? (Be gentle) Basically no matter what way i try to build the code, no matter which files, i end up getting an error one way or another and i'm ready to literally rip my hair out. [Edited by - korsen on March 24, 2008 12:03:49 AM]
Advertisement
Please edit your post and put the code in [ source ] [ /source ] tags :)

Code from books might not work on certain compilers. How old is the book? Is there a back page saying which compiler is was tested with?

Also, some books have a website address where they post updates and correct mistakes. Take a look at the back pages.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

it doesnt seem like classes.cpp is being compiled or in your project, but I could be wrong. Someone will come by and give this a sersious look don't worry.
Quote:it doesnt seem like classes.cpp is being compiled or in your project, but I could be wrong. Someone will come by and give this a sersious look don't worry.

He is compiling both classes.cpp and test.cpp, which is where the problem is at as they both define the same thing.

Quote:EDIT: Reading this code, it looks like it should be two different programs... am I just missing something here? (Be gentle)

You are linking test.cpp (test.obj) and classes.cpp (classes.h), both which define the same thing (hence the "already defined in" linker errors.
I noticed that. I removed the classes.cpp from my project and left only the .h and the test.cpp file and i still got the same error (bottom error of the two i posted)

EDIT: Which two files should i be using and what should i name them? Because trying to include classes.h in the test.cpp generates 24 errors.

EDIT2: Using JUST the test.cpp generates the same error (bottom error of the two i posted)

EDIT3: Using the first two files (classes.h and classes.cpp) gives me these two errors:

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Korsen\Documents\Visual Studio 2008\Projects\classes\Debug\classes.exe : fatal error LNK1120: 1 unresolved externals

I feel as big as this period. from all these errors bombarding me.
Quote:Original post by deadstar
Please edit your post and put the code in [ source ] [ /source ] tags :)

Code from books might not work on certain compilers. How old is the book? Is there a back page saying which compiler is was tested with?

Also, some books have a website address where they post updates and correct mistakes. Take a look at the back pages.


No backpages with any such information.
Quote:Original post by korsen
Quote:Original post by deadstar
Please edit your post and put the code in [ source ] [ /source ] tags :)

Code from books might not work on certain compilers. How old is the book? Is there a back page saying which compiler is was tested with?

Also, some books have a website address where they post updates and correct mistakes. Take a look at the back pages.


No backpages with any such information.


Any chance of the name of the book? I'm sure one of us may own it.

(And I notice you tried the source tags, you need to remove the spaces, I only put those in to demonstrate without them actually MAKING a source box. My mistake)

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Do yourself a favor and get the Mike Dawson C++ book if you are new to C++ and just starting out.
I also tried learning C++ from that book before I knew any better and spent 1/2 my time trying to get the code in that book to compile.
It was written with VC++ 6 from what I remember so it doesn't even follow the new for loop scope rules.
I could go dig up my copy and look to see if I wrote down any notes on how to fix the errors but I'm eating right now.
This review
mentions but a couple of the problems with this book.


[Edited by - daviangel on March 24, 2008 4:25:24 AM]
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Name is C++ Programming for the absolute beginner by Dirk Henkemans and Mark Lee.

It's an awesome learning tool up until chapter 5 where this crap hits me in the face like an old woman with bricks in her purse. I was breezing through the first 4 chapters within a week and it was great because of the whole gaming reference it uses throughout the book. Now i'm a bit lost.

I might just get the book so I don't waste anymore time... cause i've got 14 other books waiting to be read because i'm stuck on this one.
Quote:Original post by daviangel
Do yourself a favor and get the Mike Dawson C++ book if you are new to C++ and just starting out.
I also tried learning C++ from that book before I knew any better and spent 1/2 my time trying to get the code in that book to compile.
It was written with VC++ 6 from what I remember so it doesn't even follow the new for loop scope rules.
I could go dig up my copy and look to see if I wrote down any notes on how to fix the errors but I'm eating right now.


I found these two books:
This is from 2004
http://www.amazon.com/Beginning-C%2B%2B-Game-Programming-Development/dp/1592002056/ref=sr_1_8?ie=UTF8&s=books&qid=1206334835&sr=1-8
This is from Dec 2005 (so basically 2006)
http://www.amazon.com/Microsoft-Express-Programming-Absolute-Beginner/dp/159200816X/ref=sr_1_2?ie=UTF8&s=books&qid=1206334983&sr=1-2

would either be a good pick? if not, does anyone have excellent suggestions for something that's not anymore dry than the sahara? i really liked this book till it started giving me trouble.

I noticed the book i'm using is from 2000 :(

This topic is closed to new replies.

Advertisement