编译时报错“ undefined reference to”
来源:互联网 发布:js隐式全局变量 编辑:程序博客网 时间:2024/05/20 18:50
报错
写了一段小程序,报错“exception.cpp:(.text+0x28): undefined reference to `Sales_item::setPrice(double)“程序就算c++ prime中得测试程序:
程序代码
//*********Sales_Item.h
#ifndef SALES_H#define SALES_H#include <string>using namespace std;class Sales_item{ public: Sales_item(); Sales_item(double p,double q); inline double getPrice(); inline double getQuatity(); inline void setPrice(double p); void setQuatity(double q); double operator + (Sales_item& s2); private: string Id; double price; double quatity;};#endif
//********Sales_Item.cpp
#include "Sales_item.h" 2 #include<iostream> 3 4 //using namespace std; 5 6 Sales_item::Sales_item(){} 7 8 Sales_item::Sales_item(double p, double q):price(p),quatity(q){} 9 10 inline double Sales_item::getPrice() 11 { 12 return price; 13 } 14 inline double Sales_item::getQuatity() 15 { 16 return quatity; 17 } 18 inline void Sales_item::setPrice(double p) 19 { 20 price = p; 21 } 22 void Sales_item::setQuatity(double q) 23 { quatity = q; 25 } 26 27 double Sales_item::operator +(Sales_item& s2) 28 { 29 return this->price*this->quatity+s2.getPrice()*s2.getQuatity(); 30 }
//********* exception.cpp
#include<iostream>#include<string>#include "Sales_item.h"//using namespace std;int main(){ Sales_item sItem; sItem.setPrice(10.1); sItem.setQuatity(20); Sales_item s2(1,100); double tmpPrice = sItem.getPrice(); double tmpQuatity = sItem.getQuatity(); cout<<"the Item price is :"<<tmpPrice<<endl; cout<<"the Item price is :"<<tmpQuatity<<endl; double sum = sItem+s2; cout<<"the sum is :("<<sItem.getPrice()<<"*"<<sItem.getQuatity()<<")+("<<s2.getPrice()<<"*"<<s2.getQuatity()<<")="<<sum<<endl; return 0;}
找错
刚开始以为是g++ 我的多文件编译 命令写错了, g++ Sales_Item.cpp exception.cpp -o main
以为 g++命令找不到 cpp文件的头文件,于是把头文件的内容复制到cpp文件中重新编译,编译果然通过了.
但是直觉告诉我,问题可能不是g++错了,而是我自己都程序写错了,经过查资料,终于在c++ prime找到一段 inline相关得说明:
解析
将函数指定为 inline 函数, 通常)(就是将它在程序中每个调用点上“内联地”展开。在编译时将展开,从而消除了把 函数 写成函数的额外执行开销。inline 说明对于编译器来说只是一个建议,编译器可以选择忽略这个。一般来说,内联机制适用于优化小的、只有几行的而且经常被调用的函数。大多数的编译器都不支持递归函数的内联。一个 1200 行的函数也不太可能在调用点内联展开。把 inline 函数放入头文件内联函数应该在头文件中定义,这一点不同于其他函数。 inline 函数的定义对编译器而言必须是可见的,以便编译器能够在调用点内联展开该函数的代码。此时,仅有函数原型是不够的。inline 函数可能要在程序中定义不止一次,只要 inline 函数的定义在某个源文件中只出现一次,而且在所有源文件中,其定义必须是完全相同的。把inline 函数的定义放在头文件中,可以确保在调用函数时所使用的定义是相同的,并且保证在调用点该函数的定义对编译器可见。在头文件中加入或修改 inline 函数时,使用了该头文件的所有源文件都必须重新编译。
总结
因此我的错误并不是g++ 命令写错了,而是c++ 程序写错了,错误原因是inline函数没有在头文件种定义,而是在cpp文件定义了。改成在头文件中定义inline函数或者 定义为非inline函数,编译果然不报错,程序输出为:
the Item price is :10.1the Item price is :20the sum is :(10.1*20)+(1*100)=302
- 编译时报错:undefined reference to `pthread_create’
- 编译时报错“ undefined reference to”
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- [转]pthread_create函数编译时报错:undefined reference to 'pthread_c...
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- 解决gcc 编译时报错"undefined reference to 'main'" 问题
- qt creator编译qt工程时报错:undefined reference to
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- 【Qt开发】关于编译时报"undefined reference to"问题
- qt编译时报undefined reference to 'ts_read_raw'错误
- ios证书 配置文件 沙盒保护 代码签名
- 数据结构—利用单链表操作城市信息
- Verilog 之随机波形产生-可综合
- 大神的第一步-------开山篇
- Linux 字符设备驱动开发--内存读写操作
- 编译时报错“ undefined reference to”
- spring框架的一些了解(一)
- 无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用)
- 不排序找到第k大的值
- 排序总结(三)之---堆排序
- 禁用系统的Ctrl+Alt+Left/Right(方向键)
- I/O & Java in ACM
- 数据库知识
- MySQL事务笔记 事例回滚