我尝试使用devc ++ ver 4.9.9.2运行模板类 但它不起作用 这是因为:
编译日志:
//-------------------------------
Compiler: Default compiler
Building Makefile: "H:\BaiTap\Ky6\OPP\Test\Bai3.1_DEVC\Makefile.win"
Finding dependencies for file: H:\BaiTap\Ky6\OPP\Test\Bai3.1_DEVC\main.cpp
Finding dependencies for file: H:\BaiTap\Ky6\OPP\Test\Bai3.1_DEVC\Vecto.cpp
Executing make...
make.exe -f "H:\BaiTap\Ky6\OPP\Test\Bai3.1_DEVC\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"F:/Dev-Cpp/include/c++/3.4.2/backward" -I"F:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"F:/Dev-Cpp/include/c++/3.4.2" -I"F:/Dev-Cpp/include"
**F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h: In instantiation of `std::iterator_traits<float>':
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:96: instantiated from `std::reverse_iterator<float>'
Vecto.h:22: instantiated from `Vecto<float>'
main.cpp:9: instantiated from here
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:129: error: `float' is not a class, struct, or union type
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:130: error: `float' is not a** class, struct, or union type
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:131: error: `float' is not a class, struct, or union type
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:132: error: `float' is not a class, struct, or union type
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:133: error: `float' is not a class, struct, or union type
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h: In instantiation of `std::reverse_iterator<float>':
Vecto.h:22: instantiated from `Vecto<float>'
main.cpp:9: instantiated from here
**F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:96: error: no type named `iterator_category' in `struct std::iterator_traits<float>'**
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:103: error: no type named `difference_type' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:104: error: no type named `reference' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:105: error: no type named `pointer' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:150: error: no type named `reference' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:162: error: no type named `pointer' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:220: error: no type named `difference_type' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:229: error: no type named `difference_type' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:241: error: no type named `difference_type' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:250: error: no type named `difference_type' in `struct std::iterator_traits<float>'
F:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator.h:262: error: no type named `reference' in `struct std::iterator_traits<float>'
Vecto.h: In instantiation of `Vecto<float>':
main.cpp:9: instantiated from here
Vecto.h:22: error: template-id `operator+<float>' for `Vecto<float> operator+(const Vecto<float>&, const Vecto<float>&)' does not match any template declaration
Vecto.h:24: error: template-id `operator-<float>' for `Vecto<float> operator-(const Vecto<float>&, const Vecto<float>&)' does not match any template declaration
Vecto.h:33: error: template-id `operator<< <float>' for `std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, const Vecto<float>&)' does not match any template declaration
Vecto.h:35: error: templ
........................
make.exe: *** [main.o] Error 1
Execution terminated
//---------------------------------
这是我的代码
//-----------------------------------
#ifndef VECTO_H_
#define VECTO_H_
#include < iostream >
using namespace std;
template < class T >
class Vecto {
private:
int n;
T *data;
public:
// constructor
Vecto(int = 0);
// copy constuctor
Vecto(const Vecto&);
// destructor
~Vecto();
friend Vecto operator+ < T > (const Vecto &lvt, const Vecto &rvt);
friend Vecto operator- < T > (const Vecto &lvt, const Vecto &rvt);
template < class U >
friend U operator* (const Vecto < U > &lvt, const Vecto < U > &rvt);
const Vecto &operator= (const Vecto &);
T &operator[] (int) const;
friend ostream &operator << < T > (ostream &,const Vecto &);
friend istream &operator >> < T > (istream &,Vecto &);
};
#endif
//-----------------------------
//file Vecto.cpp
#include "Vecto.h"
using namespace std;
template < class T >
Vecto < T > ::Vecto(int n){
this- > n = n;
data = new T[n];
}
template < class T >
Vecto < T > ::Vecto(const Vecto < T > &vt){
n = vt.n;
delete [] data;
data = new T[n];
for(int i=n-1; i > =0; i--)
data[i] = vt.data[i];
}
template < class T >
Vecto < T > ::~Vecto(){
if(data != NULL)
delete[] data;
}
template < class T >
Vecto < T > operator+ (const Vecto < T > &lvt, const Vecto < T > &rvt){
Vecto < T > *p = NULL;
if(lvt.n == rvt.n){
p = new Vecto < T > (lvt.n);
for(int i=0; i < lvt.n ; i++)
p- > data[i] = lvt.data[i] + rvt.data[i];
}
return *p;
}
template < class T >
Vecto < T > operator- (const Vecto < T > &lvt, const Vecto < T > &rvt){
Vecto < T > *p = NULL;
if(lvt.n == rvt.n){
p = new Vecto < T > (lvt.n);
for(int i=0; i < lvt.n ; i++)
p- > data[i] = lvt.data[i] - rvt.data[i];
}
return *p;
}
template < class T >
T operator* (const Vecto < T > &lvt, const Vecto < T > &rvt){
T result =0;
if(lvt.n == rvt.n){
for(int i = lvt.n -1; i > =0; i--)
result += (lvt.data[i]*rvt.data[i]);
}
return result;
}
template < class T >
const Vecto < T > &Vecto < T > ::operator= (const Vecto < T > &vt){
delete []data;
n = vt.n;
data = new T[n];
for(int i=0; i < n;i++)
data[i] = vt.data[i];
return (*this);
}
template < class T >
T &Vecto < T > ::operator[] (int i) const{
T null = 0;
if(i < 0||i > =n) return null;
else return data[i];
}
template < class T >
ostream &operator < < (ostream &out, const Vecto < T > &vt){
out < < "Dims of Vecto: " < < vt.n < < endl;
out < < "Data:" < < endl;
for(int i = 0; i < vt.n; i++)
out < < vt.data[i] < < " ";
out < < endl;
return out;
}
template < class T >
istream &operator > > (istream &in, Vecto < T > &vt){
for(int i = 0; i < vt.n; i++){
cout < < "Vecto[" < < i < < "]=";
in > > vt.data[i];
}
return in;
}
//----------------------------------------------------------
//file main.cpp
#include < cstdlib >
#include < iostream >
#include "Vecto.h"
using namespace std;
int main(int argc, char *argv[])
{
Vecto < float > vt1(2);
cin > > vt1;
Vecto < float > vt2 = vt1;// - vt1;
vt2[0] = 100;
cout < < vt2;
cout < < vt2*vt1 < < endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//--------------------------------
你可以在这里下载代码。
http://www.mediafire.com/?3hknr5kj33mh4h8 谁能帮我? 感谢阅读这篇文章。
答案 0 :(得分:2)
格式化代码时应该更加小心。像这样的事情
cout < < "Hello" < < endl;
根本不起作用 - 编译器如何知道您要调用operator<<
而不是误用operator<
形式?
另外,这个
friend Vecto operator+<T> (const Vecto &lvt, const Vecto &rvt);
没有任何意义。我想你想要这个:
friend Vecto<T> operator+(const Vecto<T> &lvt, const Vecto<T> &rvt);
而且,为什么要打电话给你的班级Vecto
?您宁愿称之为Vector
,或者更好地使用std::vector
。
答案 1 :(得分:1)
必须在要使用它们的所有翻译单元中定义模板。这实际上意味着您必须在标题中定义整个类。