Weird linking error using Visual C++ 2005 express
For some reason I get a linking error when compiling my program.
------ Build started: Project: BreadthFirstSearch, Configuration: Debug Win32 ------
Linking...
State.obj : error LNK2005: "public: __thiscall State::State(int *,int)" (??0State@@QAE@PAHH@Z) already defined in main.obj
State.obj : error LNK2005: "public: __thiscall State::State(int *,int,class State)" (??0State@@QAE@PAHHV0@@Z) already defined in main.obj
State.obj : error LNK2005: "public: void __thiscall State::printState(void)" (?printState@State@@QAEXXZ) already defined in main.obj
State.obj : error LNK2005: "public: bool __thiscall State::isValidState(void)" (?isValidState@State@@QAE_NXZ) already defined in main.obj
C:\Documents and Settings\JYT\My Documents\Visual Studio 2005\Projects\BreadthFirstSearch\Debug\BreadthFirstSearch.exe : fatal error LNK1169: one or more multiply defined symbols found
Build log was saved at "file://c:\Documents and Settings\JYT\My Documents\Visual Studio 2005\Projects\BreadthFirstSearch\BreadthFirstSearch\Debug\BuildLog.htm"
I do not know what this error occurs.
I have installed platform sdk.
I have openGL and Glut installed (although this shouldnt have anything to do with the problem.)
and here is my code (sorry I dont know the commands for postin code yet)
main.cpp
#include "State.cpp"
int start[] = {3,3,0,0,0};
int startSize = 5;
void main ( void )
{
State MandC(start, startSize);
system("pause"); //used to keep comand prompt open
}
State.cpp
#include <iostream>
/***
*The state class
* stores the state of the search
****/
class State
{
private:
int *problem; //pointer to the array of the problem
State *previous; //points to previous
int size; //size of the array
public:
State(int *prob, int length); //constructor
State(int *prob, int length, State PrevState); //constructor overloading used to create a new State with previous
void printState(); //prints the state
bool isValidState(); //checks if state is valid
//~State(); //destructor
};
State::State(int *prob, int length)
{
problem = prob;
size = length;
previous = NULL;
}
State::State(int *prob, int length, State PrevState)
{
problem = prob;
size = length;
this -> previous = &PrevState
}
void State::printState()
{
for(int i=0; i<size; i++)
printf("%d, ",problem++);
}
and thank you for helping
The tags for posting source are [ source ] [ /source ] (Minus the spaces).
Your code gives linker errors because you're including a source file to another source file. That's usually a bad idea [smile]. You should only really be including header files, which should contain the prototypes for any functions, and a seperate source file should have the implementation of the files. Think of #include as a copy and paste, in your case you have two files, and with the #include expanded, you have the implementation of your State class in two files.
The correct way would be to use a header file:
Main.cpp:
State.h:
State.cpp:
EDIT: Added sentinals to the header - that stops problems occuring where a header is included into a .cpp twice (Usually by one header including another).
Your code gives linker errors because you're including a source file to another source file. That's usually a bad idea [smile]. You should only really be including header files, which should contain the prototypes for any functions, and a seperate source file should have the implementation of the files. Think of #include as a copy and paste, in your case you have two files, and with the #include expanded, you have the implementation of your State class in two files.
The correct way would be to use a header file:
Main.cpp:
#include "State.h"int start[] = {3,3,0,0,0};int startSize = 5;void main ( void ){ State MandC(start, startSize); system("pause"); //used to keep comand prompt open}
State.h:
#ifndef State_H__INCLUDED#define State_H__INCLUDEDclass State{private: int *problem; //pointer to the array of the problem State *previous; //points to previous int size; //size of the arraypublic: State(int *prob, int length); //constructor State(int *prob, int length, State PrevState); //constructor overloading used to create a new State with previous void printState(); //prints the state bool isValidState(); //checks if state is valid //~State(); //destructor};#endif // State_H__INCLUDED
State.cpp:
#include "State.h"State::State(int *prob, int length){ problem = prob; size = length; previous = NULL;}State::State(int *prob, int length, State PrevState){ problem = prob; size = length; this -> previous = &PrevState;}void State::printState(){ for(int i=0; i<size; i++) printf("%d, ",problem++);}
EDIT: Added sentinals to the header - that stops problems occuring where a header is included into a .cpp twice (Usually by one header including another).
You are compiling "state.cpp" twice, once in "main.cpp" and then again as itself - hence the multiple symbol definitions. You need to use header files, that is, a file that usually has the extension ".h". Unfortunately, the "#include" directive does allow you to include any file.
To fix your project:
main.cpp
state.cpp
state.h
There are articles on this site that describe how to set up projects and use header files and source files. Look at the Resources/Features menu option at the top of this page.
Skizz
To fix your project:
main.cpp
#include "State.h" // use a header fileint start[] = {3,3,0,0,0};int startSize = 5;void main ( void ){ State MandC(start, startSize); system("pause"); //used to keep comand prompt open}
state.cpp
#include "state.h" // use header file here as well#include <iostream>State::State(int *prob, int length){ problem = prob; size = length; previous = NULL;}State::State(int *prob, int length, State PrevState){ problem = prob; size = length; this -> previous = &PrevState;}void State::printState(){ for(int i=0; i<size; i++) printf("%d, ",problem++);}
state.h
/****The state class* stores the state of the search****/class State{private: int *problem; //pointer to the array of the problem State *previous; //points to previous int size; //size of the arraypublic: State(int *prob, int length); //constructor State(int *prob, int length, State PrevState); //constructor overloading used to create a new State with previous void printState(); //prints the state bool isValidState(); //checks if state is valid //~State(); //destructor};
There are articles on this site that describe how to set up projects and use header files and source files. Look at the Resources/Features menu option at the top of this page.
Skizz
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement