有构造函数的麻烦

Bra*_*don 0 c++ constructor header class

当我尝试构建头文件,类和构造函数时,我不断收到错误.Dev C++给了我一堆错误,我不知道如何解决它们.我在代码中包含了错误作为注释:

TEST.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;
}
Run Code Online (Sandbox Code Playgroud)

Header2.h

#ifndef Object_H_
#define Object_H_

class Object
{
 public:
        Object(int a);

        int getValue();
 private:
         int truthValue;
}

#endif // Object_H_
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我不明白.我究竟做错了什么?

sim*_*onc 5

您需要;在声明结束时使用Object

class Object
{
    ....
};
Run Code Online (Sandbox Code Playgroud)

  • @BrandonHoutzer只需将文件添加到您的项目中,IDE应该处理其余的事情.你必须在类之后有一个分号的原因是你可以在`}`和`;`之间创建变量实例,比如`class C {} c1,c2;`或`for(struct {int x = 0 ,y = 0;} s; sx <5; ++ sy)...`你需要分号告诉编译器你不会声明任何变量.**另外**Dev-C++非常古老,不值得使用; 请考虑使用[Visual C++的免费版本](http://www.microsoft.com/visualstudio/eng/downloads#d-2013-express)(Windows桌面) (2认同)