C++ 使用头文件时的显式超类构造函数问题
我似乎对调用显式超类构造函数的继承类感到非常沮丧。 我似乎无法正确理解语法!
到目前为止,我在这个问题上看到的所有示例都没有将标头和内联类定义(使用 {} 的)与头文件的前向声明分开,因此我不确定如何涵盖.h 和 .cc 文件之间的语法。 任何帮助,将不胜感激!
这是编译器给我的错误(gcc):
serverconnection.h:在 构造函数 “服务器连接::服务器连接(std::字符串, std::string)": serverconnection.h:25: 错误:输入末尾应有“{” serverconnection.cc:在全局范围内: 服务器连接.cc:20:错误: 重新定义 “服务器连接::服务器连接(std::字符串, 无符号整型、短无符号整型、 PacketSender*, int)" serverconnection.h:25:错误: “服务器连接::服务器连接(std::字符串, 无符号整型、短无符号整型、 PacketSender*, int)" 之前 此处定义 serverconnection.cc: 在 构造函数 “服务器连接::服务器连接(std::字符串, std::string)": serverconnection.cc:20: 错误:没有匹配的调用函数 到“连接::连接()”
我知道它正在尝试调用默认的连接构造函数 Connection(),因为它只是不理解我的语法。
这是代码:
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
}
提前非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不要将初始值设定项列表放在类声明中,而是放在函数定义中。 将其从标头和 .cc 文件中删除:
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:
您需要将基类初始值设定项列表从 serverconnection.h 移动到服务器connection.cc:
并且只需在标头中声明不带任何修饰的 ServerConneciton 构造函数。
You need to move the base class initializer list from serverconnection.h to server connection.cc:
And just declare the ServerConneciton constructor without any decoration in the header.
您在类声明末尾缺少分号:
忘记它可能会导致一些非常令人困惑的错误消息,并且编译器不会将错误放置在文件中,而错误确实存在。
如果您在构造函数声明/定义中提供成员初始化列表,则需要为其余的实现提供大括号,即使您没有在这些大括号中放置任何代码。 将成员初始化列表视为定义的一部分,而不是声明的一部分。
You're missing the semicolon at the end of your class declaration:
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.