Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Eclipse中构建项目时出错 当我在Eclipse中编译一个C++项目时,它会显示我的错误,说IO.cpp中的所有函数都已经定义了。_C++_Eclipse - Fatal编程技术网

在Eclipse中构建项目时出错 当我在Eclipse中编译一个C++项目时,它会显示我的错误,说IO.cpp中的所有函数都已经定义了。

在Eclipse中构建项目时出错 当我在Eclipse中编译一个C++项目时,它会显示我的错误,说IO.cpp中的所有函数都已经定义了。,c++,eclipse,C++,Eclipse,这是我的代码: 文件:IO.cpp #include <string> #include <iostream> using namespace std; void print(string line) { cout << line; } void println(string line) { cout << line << endl; } void printError(string message, strin

这是我的代码:

文件:IO.cpp

#include <string>
#include <iostream>

using namespace std;

void print(string line) {
    cout << line;
}

void println(string line) {
    cout << line << endl;
}

void printError(string message, string error, string file) {
    cout << "An error occurred!" << endl;
    cout << "Message: "+ message << endl;
    cout << "Error: "+ error << endl;
    if(file != "") {
        cout << "File/Method: "+ file << endl;
    }
}
文件:main.cpp

#include <string>
#include <iostream>
#include "IO.cpp"

using namespace std;

int main()
{
    println("Hello world!");
}

您应该从main.cpp中删除下面的行

并在使用名称空间std之后添加以下行

如果你在你的项目源列表文件中再次包含一个CPP文件,你的程序将得到相同的函数的多个定义,这些函数在C++中是不允许的。另一方面,这里建议的替代方案包括函数声明,函数声明允许多次出现,但在首次使用之前必须至少出现一次

标准做法是在ex:IO.h的头文件中移动声明,并将此头文件同时包含在IO.cpp和main.cpp中

进一步阅读:

您在module main.cpp中包含了module IO.cpp

因此,您已经获得了两个模块中的函数定义:IO.cpp和main cpp

您应该创建一个头文件,例如IO.h,并将所有函数声明放在那里。然后必须在IO.cpp和main.cpp中包含此头文件

比如说

木卫一

IO.cpp

main.cpp


规则1:不要在另一个文件中包含.cpp文件。谢谢,我认为如果IO.cpp没有像PHPThanks那样包含在main.cpp中,编译器就不会编译IO.cpp。我认为如果IO.cpp没有像PHP那样包含在main.cpp中,编译器就不会编译IO.cpp
#include "IO.cpp"
void print(string line);
void println(string line);
void printError(string message, string error, string file);
#include "IO.cpp"
#include <string>


void print( std::string line);

void println( std::string line);

void printError( std::string message, std::string error, std::string file);
#include <string>
#include <iostream>
#include <IO.h>

using namespace std;

void print(string line) {
    cout << line;
}

void println(string line) {
    cout << line << endl;
}

void printError(string message, string error, string file) {
    cout << "An error occurred!" << endl;
    cout << "Message: "+ message << endl;
    cout << "Error: "+ error << endl;
    if(file != "") {
        cout << "File/Method: "+ file << endl;
    }
}
#include <IO.h>

//...