简体   繁体   English

无法编译模板类

[英]Can't compile template class

I have try to run a template class with devc++ ver 4.9.9.2 but it didn't work because: 我尝试使用devc ++ 4.9.9.2版运行模板类,但由于以下原因而无法正常工作:

compile log: 编译日志:

//-------------------------------
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
//---------------------------------

and here is my code 这是我的代码

//-----------------------------------
#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;
}
//--------------------------------

you can download code here. 您可以在此处下载代码。

http://www.mediafire.com/?3hknr5kj33mh4h8 can any one help me? http://www.mediafire.com/?3hknr5kj33mh4h8有人可以帮我吗? thank for reading this post. 感谢您阅读这篇文章。

You should be more careful in formatting your code. 您在格式化代码时应格外小心。 Things like this 这样的事情

cout < < "Hello" < < endl;

simply do not work – how shall the compiler know that you want to call operator<< and not a misused form of operator< ? 根本不起作用–编译器如何知道您要调用operator<<而不是滥用的operator< ?形式?

Also, this 还有这个

friend Vecto operator+<T> (const Vecto &lvt, const Vecto &rvt);

Doesn't make any sense. 没有任何意义。 I assume you want this: 我想你想要这个:

friend Vecto<T> operator+(const Vecto<T> &lvt, const Vecto<T> &rvt);

And, why do you call your class Vecto ? 而且,为什么将您的班级称为Vecto You rather shall call it Vector or even better use std::vector . 您宁可将其称为Vector ,甚至最好使用std::vector

Templates must be defined in all translation units where they are to be used. 模板必须在使用它们的所有翻译单元中定义。 This effectively means that you must define the whole class in the header. 这实际上意味着您必须在标头中定义整个类。

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

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