简体   繁体   English

在 C++ 中链接内联函数的问题

[英]Problems with linking inline functions in C++

test2.h测试2.h

#ifndef TEST2_H_INCLUDED
#define TEST2_H_INCLUDED
#include "test1.h"

inline int add(int,int);

#endif // TEST2_H_INCLUDED

test2.cpp测试2.cpp

#include "test2.h"

inline int add(int a,int b){
    return a+b;
}

main.cpp主程序

#include <iostream>
#include "test2.h"

int main()
{
    std::cout << add(1,2);
    return 0;
}

Eror:错误:

warning: inline function add(int,int) used but never defined警告:使用内联函数 add(int,int) 但从未定义
undefined reference to add(int,int)add(int,int)未定义引用
ld returned 1 exit status ld 返回 1 个退出状态

But if I remove inline from the files, the code compiles and executes fine.但是如果我从文件中删除inline ,代码编译并执行正常。 What am I doing wrong?我究竟做错了什么?

Note: I have seen all the threads on stack overflow,although they were similar to my question, the answers could not solve my issue注意:我已经看到堆栈溢出的所有线程,尽管它们与我的问题相似,但答案无法解决我的问题

Using the mingw compiler with Code Blocks将 mingw 编译器与代码块一起使用

//test2.h
#ifndef TEST2_H_INCLUDED
#define TEST2_H_INCLUDED
#include "test1.h"

inline int add(int a,int b) {
    return a+b;
}

#endif // TEST2_H_INCLUDED

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

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