如何#include“file.cpp”

时间:2015-11-24 19:42:47

标签: c++

我必须以.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”

感谢所有建议 此致

1 个答案:

答案 0 :(得分:3)

#include "kod.cpp"替换为#include "kod.h",并通过赋予此签名,使您在标题中定义的函数成为一个成员函数(目前它只是一个与该类无关的自由函数):

int kod::asd(int a){

并且您必须使cpp中的签名与声明中的签名匹配。目前,您将其声明为void,但随后在定义上返回int