[英]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.