如何处理C ++链接错误 [英] How to deal with C++ linkage error
问题描述
你好以下的人是我的代码..编译好但是当它到达链接代码时这个错误虹膜;
hapter9exe42.o
chapter9exe42.o:在函数`main'中:
chapter9exe42.cpp :(。text + 0x4d):未定义引用`int SALES :: setarr< double>(double *)'
chapter9exe42.cpp :(。text + 0x6a):未定义引用`void SALES :: displayer< double>(double *,int)'
collect2:错误:ld返回1退出状态
请注意我的问题是链接终端上的代码
这里是头文件
# ifndef SANS2_H_INCLUDED
#define SANS2_H_INCLUDED
namespace SALES
{
const int quater = 4;
struct sale
{
双倍销售[quater];
双倍平均值;
最大倍数;
双分;
};
void setsale(sale&,const double [],int n);
模板< class T>
int setarr(T *);
无效展示(const sale& s);
模板< class T>
void displayer(T *,int);
}
双倍平均值(double *,int);
double max(double *,int);
double min(double *,int);
#endif // SANS2_H_INCLUDED
这里是包含函数定义的文件
#include< iostream>
#includeSANS2.h
双倍平均值(double * arr,int n)
{
double * sum = new double;
for(int i = 0; i< n; i ++)
* sum + = arr [i];
return(* sum)/ n;
}
double max(double * arr,int n)
{
double c;
c = arr [0];
for(int i = 1; i< n; i ++)
{
(arr [i]> = c)?c = arr [i]:c = C;
}
返回c;
}
double min(double * arr,int n)
{
double c;
c = arr [0];
for(int i = 1; i< n; i ++)
{
(arr [i]< = c)?c = arr [i]:c = C;
}
返回c;
}
无SALES :: setsale(SALES :: sale& s,const double arr [],int n)
{
for (int i = 0; i< n; i ++)
{
s.sales [i] = arr [i];
}
for(int i = n; i< SALES :: quater; i ++)
{
s.sales [i] = 0;
}
s.average = average(s.sales,n);
s.max = max(s.sales,n);
s.min = min(s.sales,n);
}
模板< class T>
int SALES :: setarr(T * s)
{
int i = 0;
while(std :: cin>> s [i])
i ++;
}
void SALES :: displaysale(const SALES :: sale& s)
{
std :: cout<<sales :<< s.sales<<的std :: ENDL;
std :: cout<<平均销售额:<<< s.average<< std :: endl;
std :: cout<<mininal sale:<< s.min<< std :: endl;
std :: cout<<maximal sale:<< s.max<< std :: endl;
}
模板< class T>
void SALES :: displayer(T * p,int n)
{
int i;
for(i = 0; i< n; i ++)
{
std :: cout<<element<< i + 1<< p [1] - ;<的std :: ENDL;
}
}
和这是主文件
#include< iostream>
#includeSANS2.h
static const int LEN = 100;
int main()
{
SALES :: sale po;
double arr [LEN];
int number_of_elements;
std :: cout<<嘿,你好男人:<< std :: endl;
number_of_elements = SALES :: setarr(arr);
SALES :: displayer(arr,number_of_elements);
返回0;
}
我的尝试:
我通过互联网搜索了类似问题的解决方案,我发现我将其应用到我的代码但没有改变。所以我决定把它带到这里。
感谢您的帮助..
模板需要编译成一个文件,其中的类型可以由编译器正确推导出来。上面带有函数定义的文件没有说明模板类型应该是什么。我怀疑它将默认为 int
,如模板中所建议的那样(C ++) [ ^ ]
hello guys below is my piece of code ..it compiles well but when it reaches linking the code this error irises ;
hapter9exe42.o
chapter9exe42.o: In function `main':
chapter9exe42.cpp:(.text+0x4d): undefined reference to `int SALES::setarr<double>(double*)'
chapter9exe42.cpp:(.text+0x6a): undefined reference to `void SALES::displayer<double>(double*, int)'
collect2: error: ld returned 1 exit status
please note that my problem is linking the code on the terminal
here is the header file
#ifndef SANS2_H_INCLUDED #define SANS2_H_INCLUDED namespace SALES { const int quater = 4; struct sale { double sales[quater]; double average; double max; double min; }; void setsale(sale &,const double [],int n); template<class T> int setarr(T *); void displaysale(const sale & s); template<class T> void displayer(T *,int ); } double average(double*,int ); double max(double *,int ); double min(double *,int ); #endif // SANS2_H_INCLUDED
here is the file containing the functions definitions
#include <iostream> #include "SANS2.h" double average(double * arr,int n) { double * sum= new double; for(int i=0;i<n;i++) *sum+=arr[i]; return (*sum)/n; } double max(double * arr,int n ) { double c; c = arr[0]; for(int i=1;i<n;i++) { (arr[i]>=c)?c=arr[i]:c=c; } return c; } double min(double *arr,int n ) { double c; c = arr[0]; for(int i=1;i<n;i++) { (arr[i]<=c)?c=arr[i]:c=c; } return c; } void SALES::setsale(SALES::sale & s,const double arr[],int n) { for(int i=0;i<n;i++) { s.sales[i]=arr[i]; } for(int i =n;i<SALES::quater;i++) { s.sales[i]=0; } s.average = average(s.sales,n); s.max = max(s.sales,n); s.min= min(s.sales,n); } template<class T> int SALES::setarr(T* s) { int i = 0; while(std::cin>>s[i]) i++; } void SALES:: displaysale(const SALES::sale & s) { std::cout<<"sales :"<<s.sales<<std::endl; std::cout<<"average sales :"<<s.average<<std::endl; std::cout<<"mininal sale :"<<s.min<<std::endl; std::cout<<"maximal sale :"<<s.max<<std::endl; } template<class T> void SALES::displayer(T *p,int n) { int i; for(i=0;i<n;i++) { std::cout<<"element "<<i+1<<p[i]<<std::endl; } }
and here is the main file
#include <iostream> #include "SANS2.h" static const int LEN = 100; int main() { SALES::sale po; double arr[LEN]; int number_of_elements; std::cout<<"hey man how are you man :"<<std::endl; number_of_elements = SALES::setarr(arr); SALES::displayer(arr,number_of_elements); return 0; }
What I have tried:
i've searched over the internet for solution to similar problems and the ones i found i applied it to my code but nothing changed..so i decide to take it here.
thanks for help..
The templates need to be compiled into a file where their types can be properly deduced by the compiler. The file above with the function definitions gives no indication of what the template types should be. I suspect it will default to int
as suggested in Templates (C++)[^].
这篇关于如何处理C ++链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!