c++ - 构造函数有问题

标签 c++ class constructor header

当我尝试构建 header 、类和构造函数时,我总是遇到错误。 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;
}

Header2.h

#ifndef Object_H_
#define Object_H_

class Object
{
 public:
        Object(int a);

        int getValue();
 private:
         int truthValue;
}

#endif // Object_H_

Header2.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++ - 构造函数有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895767/

相关文章:

javascript - 一个数组中的对象字面量和对象构造函数是否存在潜在问题?

python - 在包中定义类

c++ - 使用 new 与不使用 new 实例化对象之间有什么区别

java - 您应该使用父类(super class)构造函数来设置变量吗?

oop - F# 对象构造函数

c++ - 从 C++ 为字符串函数设置 _ENV

C++ 类对象函数

c++ - 如何将 "cout"、 "cerr"、 "stdout"和 "stderr"从 C++ 程序定向到单个文件

python - 我应该从类内部创建一个类实例吗?

c++ - 传递给对象时获取数组的长度