【发布时间】:2014-11-22 11:51:33
【问题描述】:
这是代码:
class cat
{
private:
int height;
public:
cat (int inputHeight);
};
cat::cat (int inputHeight)
{
height = inputHeight;
}
class twoCats
{
private:
cat firstCat;
cat secondCat;
public:
twoCats (cat theFirstCat);
void addSecondCat (cat theSecondCat);
};
twoCats::twoCats (cat theFirstCat)
{
firstCat = theFirstCat;
}
void twoCats::addSecondCat (cat theSecondCat)
{
secondCat = theSecondCat;
}
int main() {return 0;}
这些是错误:
main.cpp: In constructor ‘twoCats::twoCats(cat)’:
main.cpp:24:34: error: no matching function for call to ‘cat::cat()’
main.cpp:24:34: note: candidates are:
main.cpp:9:1: note: cat::cat(int)
main.cpp:9:1: note: candidate expects 1 argument, 0 provided
main.cpp:1:7: note: cat::cat(const cat&)
main.cpp:1:7: note: candidate expects 1 argument, 0 provided
main.cpp:24:34: error: no matching function for call to ‘cat::cat()’
main.cpp:24:34: note: candidates are:
main.cpp:9:1: note: cat::cat(int)
main.cpp:9:1: note: candidate expects 1 argument, 0 provided
main.cpp:1:7: note: cat::cat(const cat&)
main.cpp:1:7: note: candidate expects 1 argument, 0 provided
我不明白以下内容:
- 为什么
twoCats的构造函数会尝试调用cat的默认构造函数?当然,它不需要构造cat的实例,因为当twoCats被初始化时,它将传递一个已经初始化的cat实例,该实例将传递int height参数? - 为什么同一块错误消息显示两次?我在 Ubuntu 12.04 上调用了
g++ main.cpp。
【问题讨论】:
-
首先,你没有在你的构造函数中初始化任何东西。您分配给已经初始化的对象。其次,您只尝试初始化
firstCat。secondCat还能用什么? -
这已经被回答过无数次了。
-
@juanchopanza 至少你必须承认 OP 在第一枪就得到了正确的问题格式;) ...
-
@thang 这将是一项艰巨的工作,人类审稿人甚至很难在 SO 上找到合适的骗子。
-
我感觉 OP 可能在笑着想“一群书呆子在分析我的家庭作业”
标签: c++ compiler-errors g++