[英]How to #include "file.cpp"
我必须包含 .cpp 格式的文件。
为了测试,我创建了一些包含示例内容的文件 1. 文件
main.cpp
#include <iostream>
#include "kod.cpp"
int main(int argc, char *argv[])
{
int x=42;
std::cout << x <<std::endl;
std::cout << asd(x) << std::endl;
return 0;
}
2.档案
kod.h
#ifndef KOD_H
#define KOD_H
class kod
{
public:
void asd(int);
protected:
};
#endif
3.档案
kod.cpp
#include <iostream>
#include "kod.h"
int asd(int a){
return ++a;
}
我不知道为什么它不起作用。 我收到来自 Dev-C++ 5.11 的错误
kod.cpp:(.text+0x0): multiple definition of `asd(int)'
main.cpp:(.text+0x0): first defined here
[Error] ld returned 1 exit status
Makefile.win recipe for target 'Projekt5.exe' failed
我如何修复这个应用程序。 我必须在 main.cpp 中使用 #define "kod.cpp"
感谢所有建议
将#include "kod.cpp"
替换为#include "kod.h"
并通过给它这个签名使您在头文件中定义的函数成为成员函数(目前它只是一个与类无关的自由函数) :
int kod::asd(int a){
并且您必须使 cpp 中的签名与声明中的签名匹配。 目前您将其声明为void
但随后在定义上它返回一个int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.