Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用头文件时出现显式超类构造函数问题_C++_C_Constructor_Super - Fatal编程技术网

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

C++ C++;使用头文件时出现显式超类构造函数问题,c++,c,constructor,super,C++,C,Constructor,Super,对于一个继承类调用显式超类构造函数,我似乎感到非常沮丧。我只是不太明白语法 到目前为止,我所看到的关于这个问题的所有示例都没有将头文件和内联类定义(使用{})与头文件的转发声明分开,因此我不确定如何涵盖.h和.cc文件之间的语法。任何帮助都将不胜感激 以下是编译器给我的错误(gcc): serverconnection.h:In 建造师 “服务器连接::服务器连接(标准::字符串, std::string)“:serverconnection.h:25: 错误:输入结束时应为“{” server

对于一个继承类调用显式超类构造函数,我似乎感到非常沮丧。我只是不太明白语法

到目前为止,我所看到的关于这个问题的所有示例都没有将头文件和内联类定义(使用{})与头文件的转发声明分开,因此我不确定如何涵盖.h和.cc文件之间的语法。任何帮助都将不胜感激

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

serverconnection.h:In 建造师 “服务器连接::服务器连接(标准::字符串, std::string)“:serverconnection.h:25: 错误:输入结束时应为“{” serverconnection.cc:在全局范围内: serverconnection.cc:20:错误: 重新定义 “服务器连接::服务器连接(标准::字符串, 无符号整数,短无符号整数, 包裹寄件人*,整数“ serverconnection.h:25:错误: “服务器连接::服务器连接(标准::字符串, 无符号整数,短无符号整数, PacketSender*,int)“以前 此处定义serverconnection.cc:在中 建造师 “服务器连接::服务器连接(标准::字符串, std::string)“:serverconnection.cc:20: 错误:没有用于调用的匹配函数 连接到“Connection::Connection()”

我知道它试图调用默认的连接构造函数Connection(),因为它不理解我的语法

代码如下:

连接.h:

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

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

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

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

非常感谢!您没有将初始值设定项列表放在类声明中,而是放在函数定义中。请将其从标头和.cc文件中删除:

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

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

您没有将初始值设定项列表放在类声明中,而是放在函数定义中。请将其从标头和.cc文件中删除:

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

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

您需要将基类初始值设定项列表从serverconnection.h移动到server connection.cc:

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

只需声明ServerConneciton构造函数,而不在头中添加任何修饰。

您需要将基类初始值设定项列表从serverconnection.h移动到server connection.cc:

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

只需在标题中声明ServerConneciton构造函数,而不进行任何修饰。

类声明末尾缺少分号:

class Connection {
    public:
       Connection(string myOwnArg);
}; // semicolon here
忘记这一点可能会导致一些非常混乱的错误消息,并且编译器不会将错误放在文件中,因为错误确实存在


如果在构造函数声明/定义中提供了成员初始化列表,则需要为实现的其余部分提供大括号,即使没有在这些大括号中放入任何代码。将成员初始化列表视为定义的一部分,而不是声明的一部分。

在e处缺少分号您的班级声明的第二部分:

class Connection {
    public:
       Connection(string myOwnArg);
}; // semicolon here
忘记这一点可能会导致一些非常混乱的错误消息,并且编译器不会将错误放在文件中,因为错误确实存在


如果在构造函数声明/定义中提供了成员初始化列表,则需要为实现的其余部分提供大括号,即使没有在这些大括号中放入任何代码。将成员初始化列表视为定义的一部分,而不是声明的一部分。

哎呀,很抱歉,这实际上是一个割裂-粘贴问题,因为实际代码要大得多。但是眼睛好!哎呀,很抱歉,这实际上是一个剪切粘贴问题,因为实际代码要大得多。但是眼睛好!现在工作得很好!非常感谢你的快速回答现在工作得很好!非常感谢你的快速回答谢谢!我想你比Jeff快了13秒,所以我会打分你的答案已经被接受了(抱歉杰夫!)谢谢!由于某些原因,绝大多数C++教程都不显示使用分离的H和CC文件时的情况…谢谢!我想你打了杰夫13秒,所以我会把你的答案标记为接受(对不起杰夫!)谢谢!由于某些原因,绝大多数C++教程没有显示使用分离的H和CC文件时的情况。