C++构造函数报错排查:显式类型缺失与构造函数实例不存在
问题解决:C++构造函数识别错误
你的问题根源在于构造函数的名称必须与类名完全一致:
- 你定义的函数名为
Chan,但类名是Ytchannel,编译器无法将其识别为构造函数,因此提示需要添加返回类型; - main函数中尝试实例化
Ytchannel对象时,找不到匹配的构造函数,因为Chan并不是Ytchannel的构造函数。
修正后的代码如下:
#include <iostream> using namespace std; class Ytchannel { public: int subscribers; string ownerName; string creationDate; string channelName; // 构造函数名改为与类名一致的Ytchannel Ytchannel(string ownerNam, string creationDat, string channelNam, int subscriber) { ownerName = ownerNam; creationDate = creationDat; subscribers = subscriber; channelName = channelNam; } }; int main() { Ytchannel sin("Sam", "7/4/2020", "SamSmees", 1029); }
修正关键点:将构造函数的名称从Chan改为Ytchannel,这样编译器就能正确识别这是Ytchannel类的构造函数,main函数中的对象实例化也能匹配到对应的构造函数,两个报错都会消失。
内容的提问来源于stack exchange,提问作者ComplexJS
相关产品推荐
相关产品推荐

