Problems with Class Constructors

Apr 10, 2009 at 4:21am
I cannot seem to figure out what I am doing wrong here. I have a lot more code I am trying to get working, but I get the same errors with this simple code, so I figure I will start there. I am copying this almost directly from the book I am using, so I am totally lost on this one.

Code

#include <iostream>
using namespace std;
class Server
{
public:
Server(char letterName);
};
int main()
{
Server sq('A'), s2('B');
return 0;
}

After this, it will not compile. I get the error:
[Linker Error]Undefined reference to 'Server::Server[char]
[Linker Error]Undefined reference to 'Server::Server[char]
Id returned 1 exit status

I am using DevC++ and it is my only compiler I have. Any help is appreciated.

Thanks
Apr 10, 2009 at 4:22am
You have to define what the constructor does. Currently, you are just saying "it exists" (prototyping it), and not defining it, causing the linker to complain.
Last edited on Apr 10, 2009 at 4:22am
Apr 10, 2009 at 3:29pm
Ah, I knew it was something like that. The book has terrible explainations, and it is pretty hard to try and read through.

Does something like this look properly set-up? This one compiles fine, so unless I am missing something else, I think I can run with this format.

#include <iostream>
using namespace std;

class Server
{
public:
Server(char);
char g;
};

Server::Server(char a)
{
g = a;
}

int main()
{
Server sq('A'), s2('B');
return 0;
}


Thanks a lot for the help!!!
Apr 10, 2009 at 3:30pm
Yes, that would work.
Topic archived. No new replies allowed.