C++ 对“操作员”的未定义引用>&燃气轮机;(标准:istream&;,LZespolona&;)&x27;
当我尝试创建LZespolona类型Wektor时,收到两条错误消息:C++ 对“操作员”的未定义引用>&燃气轮机;(标准:istream&;,LZespolona&;)&x27;,c++,templates,operator-overloading,istream,ostream,C++,Templates,Operator Overloading,Istream,Ostream,当我尝试创建LZespolona类型Wektor时,收到两条错误消息: undefined reference to `operator>>(std::istream&, LZespolona&)'| undefined reference to `operator<<(std::ostream&, LZespolona const&)' Wektor.hh: #include "rozmiar.h" #include "lzespolo
undefined reference to `operator>>(std::istream&, LZespolona&)'|
undefined reference to `operator<<(std::ostream&, LZespolona const&)'
Wektor.hh:
#include "rozmiar.h"
#include "lzespolona.hh"
#include <iostream>
template<typename typ>
class Wektor
{
typ tabwektor[ROZMIAR];
public:
typ operator [] (int xyz) const {return tabwektor[xyz];}
typ& operator [] (int xyz) {return tabwektor[xyz];}
};
template<typename typ>
std::istream& operator >> (std::istream &Strm, Wektor<typ> &Wek)
{
for (int i=0; i<ROZMIAR; i++)
{
std::cin >> Wek[i];//undefined reference to `operator>>(std::istream&, LZespolona&)'|
}
return Strm;
}
template<typename typ>
std::ostream& operator << (std::ostream &Strm, Wektor<typ> &Wek)
{
for (int i=0; i<ROZMIAR; i++)
{
std::cout << Wek[i]<<" "; //undefined reference to `operator<<(std::ostream&, LZespolona const&)'
}
std::cout<<std::endl;
return Strm;
}
#包括“rozmiar.h”
#包括“lzespolona.hh”
#包括
模板
韦克托级
{
tabwektor[ROZMIAR]型;
公众:
典型运算符[](int-xyz)常量{return tabwektor[xyz];}
类型运算符[](int-xyz){return tabwektor[xyz];}
};
模板
std::istream&operator>>(std::istream&Strm、Wektor&Wek)
{
for(int i=0;i>Wek[i];//对`operator>>(std::istream&,LZespolona&)的未定义引用|
}
返回Strm;
}
模板
std::ostream&operator你定义过这个函数吗?我在LZespolona.h
中看到流操作符的声明,但是定义在哪里?我在LZespolona.cpp中有定义,我将更新op。你是如何编译程序的?“你是如何编译程序的?”好的,这是个好问题,代码块(GNU GCC)造成问题的原因。当我创建自己的makefile时,一切都正常。问题已解决:)
#include "rozmiar.h"
#include "lzespolona.hh"
#include <iostream>
template<typename typ>
class Wektor
{
typ tabwektor[ROZMIAR];
public:
typ operator [] (int xyz) const {return tabwektor[xyz];}
typ& operator [] (int xyz) {return tabwektor[xyz];}
};
template<typename typ>
std::istream& operator >> (std::istream &Strm, Wektor<typ> &Wek)
{
for (int i=0; i<ROZMIAR; i++)
{
std::cin >> Wek[i];//undefined reference to `operator>>(std::istream&, LZespolona&)'|
}
return Strm;
}
template<typename typ>
std::ostream& operator << (std::ostream &Strm, Wektor<typ> &Wek)
{
for (int i=0; i<ROZMIAR; i++)
{
std::cout << Wek[i]<<" "; //undefined reference to `operator<<(std::ostream&, LZespolona const&)'
}
std::cout<<std::endl;
return Strm;
}
std::ostream& operator << ( std::ostream& StrmWy,
const LZespolona& WyswietlanaLiczba
std::istream& operator >> ( std::istream&StrmWe, LZespolona &zespolona) ;
std::ostream& operator << ( std::ostream& StrmWy,
const LZespolona& WyswietlanaLiczba)
{
std::cout<< WyswietlanaLiczba.Re();
if(WyswietlanaLiczba.Im() < 0)
{
std::cout<<"";
}
else
{
std::cout<<"+";
}
std::cout << WyswietlanaLiczba.Im() << "i" << " ";
return StrmWy;
}
istream&operator >> (istream&StrmWe, LZespolona &zespolona)
{
char Znak;
cin.get();
StrmWe >> zespolona.Re();
StrmWe >> Znak;
if(Znak == '+')
{
StrmWe >> zespolona.Im();
}
if(Znak == '-')
{
StrmWe.putback(Znak);
StrmWe >> zespolona.Im();
}
cin.get();
cin.get();
cin.get();
return StrmWe;
}