2016-07-20 27 views
0

我最近刚开始使用C单独的类文件++乱搞,这是我第一次尝试:C++类的头和实现错误

首先,我做了一个类头名为“ThisClass.h”:

//ThisClass.h 

#ifndef THISCLASS_H 
#define THISCLASS_H 

class ThisClass 
{ 
private: 
    int x; 
    float y; 

public: 
    ThisClass(int x, float y); 
    void setValues(int x, float y); 
    int printX(); 
    float printY(); 
}; 
#endif // THISCLASS_H 

然后,我实现了在一个名为“ThisClass.cpp”文件我的课:

//ThisClass.cpp 

#include "ThisClass.h" 

ThisClass::ThisClass(int x, float y) 
{ 
    this->x = x; 
    this->y = y; 
} 

void ThisClass::setValues(int x, float y) 
{ 
    this->x = x; 
    this->y = y; 
} 

int ThisClass::printX() 
{ 
    return this->x; 
} 
float ThisClass::printY() 
{ 
    return this->y; 
} 

最后,我做了一个名为“main.cpp的”,我使用的类文件:

//main.cpp 

#include <iostream> 

    using namespace std; 

    int main() 
    { 
     ThisClass thing(3, 5.5); 
     cout << thing.printX() << " " << thing.printY()<< endl; 
     thing.setValues(5,3.3); 
     cout << thing.printX() << " " << thing.printY()<< endl; 
     return 0; 
    } 

我然后编译和运行通过的代码块使用MinGW的编译器和接收以下错误此程序:

In function 'int main()':| 
main.cpp|7|error: 'ThisClass' was not declared in this scope| 
main.cpp|7|error: expected ';' before 'thing'| 
main.cpp|8|error: 'thing' was not declared in this scope| 
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 

难道我莫名其妙地这样做不对?任何帮助,将不胜感激。

+0

修复了我的错误。但是,现在我的控制台不输出任何东西,即使我有2个cout调用。 –

+0

什么控制台?您是否使用Visual Studio或其他IDE或命令行? – SurvivalMachine

+0

我正在使用使用MinGW编译器的代码块。 –

回答

2

您忘记了#include "ThisClass.h"main.cpp

0

正如已经回答你忘了把#include "ThisClass.h"放在main.cpp

只要做到这一点,你的代码将被编译。 我只是想回答你的问题 - 不过,现在我的控制台不输出任何东西,即使我有2 COUT调用 请把getchar()returnmain功能,它可以让你看到你的输出。

+0

感谢您的回复,但是控制台不工作的真正原因是因为我添加了标志-mwindows,它添加​​了禁用控制台的库。 –