遇到构造函数问题
Having trouble with constructors
当我尝试构建标头、类和构造函数时,我总是收到错误。Dev C++给了我一堆错误,我不知道如何解决它们。我将错误作为注释包含在代码中:
测试.cpp
#include <iostream>
#include <conio.h>
#include "Header2.h"
int main()
{ //ERROR: new types may not be defined in a return type; extraneous `int' ignored;
// `main' must return `int'
Object Thing(1);
std::cout << "The truth value is: " Thing.getValue() << std::flush << "/n";
//ERROR: ISO C++ forbids declaration of `getValue' with no type
getch();
return 0;
}
标题2.h
#ifndef Object_H_
#define Object_H_
class Object
{
public:
Object(int a);
int getValue();
private:
int truthValue;
}
#endif // Object_H_
标题2.cpp
#include <iostream>
#include "Header2.h"
Object::Object(int a)
{ //ERROR: new types may not be defined in a return type;
// return type specification for constructor invalid
if (a != 0 || a !=1)
{
std::cout << "Improper truth value." << std::flush;
} else
{
truthValue = a;
}
}
Object::getValue()
{ //Error: ISO C++ forbids declaration of `getValue' with no type
return truthValue;
}
我不明白。我做错了什么?
您需要
在声明末尾;
Object
class Object
{
....
};
相关文章:
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- constexpr构造函数需要常量成员函数时出现问题
- 有关 c++ 构造函数的问题
- 关于复制构造函数的一个棘手问题
- 复制构造函数隐式转换问题
- 构造函数 (C++) 中的 char 指针参数存在问题
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 关于 std::bitset 构造函数的几个问题?
- 构造函数和 G++ 编译配方的问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- C++ 关于两个类构造函数的问题
- 在构造函数 c++ 中初始化属性时出现问题
- 空构造函数和值传递问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- 复制和赋值构造函数的问题
- 变分模板递归构造函数问题
- C++中用户定义的构造函数出现问题
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- C++的抽象类继承和构造函数的问题
- 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?