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!
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: