使用头文件时C++显式超类构造函数问题

1 c c++ constructor super

我似乎在继承类调用显式超类构造函数时遇到了非常令人沮丧的时间.我似乎无法正确使用语法!

到目前为止,我在这个问题上看到的所有例子都没有将头部和内联类定义(使用{}')与带头文件的前向声明分开,所以我不知道如何覆盖.h和.cc文件之间的语法.任何帮助,将不胜感激!

这是编译器给我的错误(gcc):

serverconnection.h:在构造函数"ServerConnection :: ServerConnection(std :: string,std :: string)"中:serverconnection.h:25:错误:输入serverconnection.cc末尾的预期`{':全局范围:serverconnection. cc:20:错误:重新定义"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)"之前在这里定义的serverconnection.cc:在构造函数中"ServerConnection :: ServerConnection(std :: string,std :: string)":serverconnection.cc:20:错误:否调用"Connection :: Connection()"的匹配函数

我知道它正在尝试调用默认的Connection构造函数Connection(),因为它只是不理解我的语法.

这是代码:

connection.h:

class Connection {
    public:
       Connection(string myOwnArg);
};
Run Code Online (Sandbox Code Playgroud)

connection.cc:

#include "connection.h"
Connection::Connection(string myOwnArg) {
     //do my constructor stuff
}
Run Code Online (Sandbox Code Playgroud)

serverconnection.h:

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

serverconnection.cc:

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

非常感谢提前!

jfc*_*tte 5

您没有将初始化列表放在类声明中,而是放在函数定义中.从标题和.cc文件中删除它:

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

ServerConnection::ServerConnection(string myOwnArg, string superClassArg) : Connection(superClassArg) {
     //do my constructor stuff
}
Run Code Online (Sandbox Code Playgroud)