我在创建 Hello World 的 OOP 版本时遇到问题。如何将 World 对象定义添加到我的标题中?
主要.cpp:
#include "stdafx.h"
#include "World.h"
#include <iostream>
int main() {
World world;
World world2(5);
std::cin.get();
return 0;
}
世界.cpp:
#include "stdafx.h"
#include <iostream>
class World {
public:
World() {
std::cout << "Hello World!" << std::endl;
}
World(int test) {
std::cout << "Hello World!" << std::endl;
}
~World() {
std::cout << "Good Bye!" << std::endl;
}
};
世界.h:
class World;
class World(int);
错误:
1>d:\programming\c++\consoleapplication1\consoleapplication1\world.h(2): error C2062: type 'int' unexpected
1>d:\programming\c++\consoleapplication1\consoleapplication1\main.cpp(6): error C2079: 'world' uses undefined class 'World'
1>d:\programming\c++\consoleapplication1\consoleapplication1\main.cpp(7): error C2079: 'world2' uses undefined class 'World'