I'm having trouble properly defining and using headers. To preface, I'm a Java programmer just starting to learn C++. Basically, what I'm trying to do is to create a new Character and then make sure the functions are working (default constructor, parameterized constructor, setStr, and getStr). Here's the main class, source, and header files:
Character.h:
1 2 3 4 5 6 7 8 9 10 11
class Character{
private:
int str;
public:
void setStr(int i);
int getStr();
Character();
Character(int i);
};
Character.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "Character.h"
int str;
void setStr(int i){
str = i;
}
int getStr(){
return str;
}
Character :: Character(){
Character(-1);
}
Character :: Character(int i){
setStr(i);
}
main.cpp:
1 2 3 4 5 6 7 8 9 10 11
#include "Character.h"
#include <iostream>
usingnamespace std;
int main(char args, char* argv){
int stall;
Character ch;
cout << ch.getStr();
cin >> stall;
}
build log:
1 2 3 4 5 6
1> main.cpp
1> Character.cpp
1> Generating Code...
1>Character.obj : error LNK2019: unresolved external symbol "public: void __thiscall Character::setStr(int)" (?setStr@Character@@QAEXH@Z) referenced in function "public: __thiscall Character::Character(int)" (??0Character@@QAE@H@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Character::getStr(void)" (?getStr@Character@@QAEHXZ) referenced in function _main
1>c:\users\jacob\documents\visual studio 2010\Projects\Balance\Debug\Balance.exe : fatal error LNK1120: 2 unresolved externals
I'm unclear exactly which bits of information goes into the header and which goes into the source, I'm guessing the error is a result of having some definitions in the wrong file, but basic guessing and checking and google-fu have been fruitless.
That fixed it up, thanks. As a follow up question, why do I not need to use
usingnamespace Character;
in the main method and I'm still able to reference stuff from Character directly, while I have to use std::cout if I don't declare that I'm using the standard namespace?