简体   繁体   中英

How to #include "file.cpp"

I must include file in .cpp format.

For test i create some file with sample content 1. file

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

kod.h
#ifndef KOD_H
#define KOD_H

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

#endif

3. file

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

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

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 :

int kod::asd(int a){

And you have to make the signature in the cpp match the signature in the declaration. At the moment you declare it to be void but then on the definition it returns an int .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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