its time that i got the hang of header and sourcefiles, only my code doesnt work, can you help?

Nov 11, 2012 at 6:51pm
Got to get out of that beginner habit i havny bothered in ages and more advanced tutorials are beginning to get indecipherable, i have a few questions, the first is should main always be called main? loads of my practice codes would wind up being called main, do i have to put them in their own folders and deal with it or can i call main anything? just worked out its fine so long as i include the header as i was typing, forget that, the other question is; is that the point of a project i can just go ahead and include different functions i have made before so soon enough all i have to do to make something is link stuff together? that sounds ace, should i start building up a project and lots of source and header files, is there a tut out there for this? if no its good advice for a fresh and bold beginner.

oh yeah and the function body in main, that could go in the source file, do i put the whole thing in there, wait how do i call it? i know why i got an error, its because i de=idnt use the scope resolution in main, is that it??


Kay, So the two errors i get are undefined referene to undefined reference to bunny::setname (std::string)the program spits out randomly created names.

and undefined reference to bunny::setname

heres the main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include<string>
#include<ctime>
#include<cstdlib>
#include "bunny namegenerator.h"
using namespace std;

main()
{
srand(time(0));
bunny bun1;
bun1.setname (namelist());
cout << bun1.getname()<<endl;
}


int getrand ()
{


return 1+(rand()%6);

}

string namelist ()
{
string namearray [18] = {"frufru ","baxter ","voldermort ","falafal ",
"fluffles "," flopsy "," panhandle ",
" bruchetta "," cottonflop "," serious ",
" bouviay "," lightening "," cellotape "," bonapart "," jackson "," gatsby "," junior "," the first "};

string none = namearray [getrand()%18];
string ntwo =namearray [getrand()*4%18];
string nthree=namearray [getrand()*7%18];
string fullname=none+ntwo+nthree;
return fullname;
}


here is bunny namegenerator.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include<string>
#include<ctime>
#include<cstdlib>


class bunny{
public:
void setname (std::string x);

std::string getname();

private:
std::string name;
};


std::string namelist ();

int getrand ();


and heres bunny namegenorator.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "bunny namegenerator.h"


void bunny::setname (string x)
{
    name = x;
}
string bunny::getname()
{
    return name;
}
Last edited on Nov 11, 2012 at 6:54pm
Nov 11, 2012 at 7:49pm
now ive changed a few bits and pieces but why are get and set undefined??
Nov 11, 2012 at 7:55pm
undefined means the linker cannot find the compiled object file containing it. The questions you ask yourself are:

1) Did I write the functions correctly? Correct name, correct parameters.
2) Did I compile the code containing the function definitions?
3) Did I tell the linker to link against it?
Nov 11, 2012 at 8:05pm
how do linkers work?

mine keeps saying it cant find SDL_main what do i tell it instead?
Nov 11, 2012 at 8:08pm
You tell it the name and location of the library containing that function.
Nov 11, 2012 at 8:13pm
erm
Nov 11, 2012 at 8:14pm
You're using some kind of program to build your code. Maybe some kind of IDE? It has settings.
Nov 11, 2012 at 8:27pm
yeah i managed to install sdl, im using codeblocks theres libraries in mingw? do i just add another address to somewhere else, is there someway i can avoiddeleting old linker options
Nov 11, 2012 at 8:44pm
what do i write in what linker section i know nothing
Topic archived. No new replies allowed.