简体   繁体   English

如何#include "file.cpp"

[英]How to #include "file.cpp"

I must include file in .cpp format.我必须包含 .cpp 格式的文件。

For test i create some file with sample content 1. file为了测试,我创建了一些包含示例内容的文件 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. file 2.档案

kod.h
#ifndef KOD_H
#define KOD_H

class kod
{
    public:
        void asd(int);
    protected:
};

#endif

3. file 3.档案

kod.cpp
#include <iostream>
#include "kod.h"   
int asd(int a){
    return ++a; 
}

I have not idea why it dosen't work.我不知道为什么它不起作用。 I recieved errors from Dev-C++ 5.11我收到来自 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

How I can repair this app.我如何修复这个应用程序。 I must use #define "kod.cpp" in main.cpp我必须在 main.cpp 中使用 #define "kod.cpp"

Thank's for all advices Regards感谢所有建议

Replace #include "kod.cpp" with #include "kod.h" and make the function you define in the header a member function (at the moment it is just a free function with no relation to the class) by giving it this signature :#include "kod.cpp"替换为#include "kod.h"并通过给它这个签名使您在头文件中定义的函数成为成员函数(目前它只是一个与类无关的自由函数) :

int kod::asd(int a){

And you have to make the signature in the cpp match the signature in the declaration.并且您必须使 cpp 中的签名与声明中的签名匹配。 At the moment you declare it to be void but then on the definition it returns an int .目前您将其声明为void但随后在定义上它返回一个int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM