views:

886

answers:

3

Hello,

I seem to be having a very frustrating time with an inherited class calling an explicit superclass constructor. I just can't seem to get the syntax right!

All the examples I have seen on the matter so far do not separate out the header and in-line class definition (using {}'s) from forward-declarations with a header file, so I'm not sure of how to cover the syntax between the .h and .cc files. Any help would be appreciated!

Here is the error the compiler gives me (gcc) :

serverconnection.h: In constructor "ServerConnection::ServerConnection(std::string, std::string)": serverconnection.h:25: error: expected `{' at end of input serverconnection.cc: At global scope: serverconnection.cc:20: error: redefinition of "ServerConnection::ServerConnection(std::string, unsigned int, short unsigned int, PacketSender*, int)" serverconnection.h:25: error: "ServerConnection::ServerConnection(std::string, unsigned int, short unsigned int, PacketSender*, int)" previously defined here serverconnection.cc: In constructor "ServerConnection::ServerConnection(std::string, std::string)": serverconnection.cc:20: error: no matching function for call to "Connection::Connection()"

I understand that it is trying to call the default Connection constructor, Connection(), as it just doesn't understand my syntax.

Here is the code:

connection.h :

class Connection {
    public:
       Connection(string myOwnArg);
};

connection.cc :

#include "connection.h"
Connection::Connection(string myOwnArg) {
     //do my constructor stuff
}

serverconnection.h :

#include "connection.h"
class ServerConnection : public Connection {
    public:
       ServerConnection(string myOwnArg, string superClassArg) : Connection(superClassArg);
};

serverconnection.cc :

#include "serverconnection.h"
#include "connection.h"
ServerConnection::ServerConnection(string myOwnArg, string superClassArg) {
     //do my constructor stuff
}

Thanks so much in advance!

+4  A: 

You don't put the initializer list in the class declaration, but in the function defininition. Remove it from the header, and in your .cc file:

#include "serverconnection.h"
#include "connection.h"

ServerConnection::ServerConnection(string myOwnArg, string superClassArg) : Connection(superClassArg) {
     //do my constructor stuff
}
jfclavette
Thanks! I guess you beat Jeff by 13 seconds so I will mark your answer as accepted (sorry Jeff!)
+2  A: 

You need to move the base class initializer list from serverconnection.h to server connection.cc:

ServerConnection::ServerConnection(string myOwnArg, string superClassArg) 
    : Connection(superClassArg) {
     //do my constructor stuff
}

And just declare the ServerConneciton constructor without any decoration in the header.

jeffamaphone
Works perfectly now! Thanks so much for your quick answer
A: 

You're missing the semicolon at the end of your class declaration:

class Connection {
    public:
       Connection(string myOwnArg);
}; // semicolon here

Forgetting that can lead to some very confusing error messages, and the compiler won't place the error in the file there the error really is.

And if you provide a member-initialization list in your constructor declaration/definition, you need to provide the braces for the rest of the implementation, even if you don't put any code in those braces. Think of the member-initialization list as part of the definition, not part of the declaration.

Rob Kennedy
Whoops sorry that was actually a cut-and-paste issue as the actual code is much more massive. But good eye!