0

我在使用模板类编译我的第一个项目时遇到问题,所有错误都源于我的模板类,包括:

In file included from CountArray.h:1:0,
             from Executive.h:2,
             from main.cpp:3:
Count.h:10:5: error: ISO C++ forbids declaration of 'T' with no type [-fpermissive]
   T();
     ^
Count.h:10:3: error: declaration of 'int Count<T>::T()'
   T();
   ^
Count.h:4:11: error:  shadows template parm 'class T'
 template <typename T>
           ^
Count.h:11:4: error: typedef-name 'T' used as destructor declarator
   ~T();
    ^
Count.h:11:6: error: declaration of '~T' as member of 'Count<T>'
   ~T();
      ^
Count.h:12:3: error: 'T' does not name a type
   T getItem();
   ^

和其他人一样。我有两个模板类,

template <typename T>
Count<T>::T(){
item=new Count<T>;
count=1;
}  

template <typename T>
Count<T>::~T(){
delete item;
}

template <typename T>
//needs type declaration, error for it being T type
T Count<T>::getItem(){
return item;
}

template<typename T>
void Count<T>::setCount(){
count++;
}

template <typename T>
int Count<T>::getCount(){
    return count;
}

另一个有头文件

#include "Count.h"
#ifndef COUNTARRAY_H
#define COUNTARRAY_H
template <typename T>
class CountArray{
private:
    Count<T>* array;
    int arraySize; // initially: 10
    int numItemsStored=0;
public:
    CountArray();
    CountArray(const CountArray<T>& countArr);
    ~CountArray();

    void bumpCount(T t);
    int getNumItemsStored() const;
    Count<T> getItem(int whichItem) const;
    int getCount(int whichItem) const;
    void print();
    int setArraySize(int size);
    int getArraySize();
    void doubleArraySize();
};

#include "CountArray.cpp"
#endif

其中有Executive调用的实例:

#include <fstream>
#include "CountArray.h"

class Executive{
private:
    CountArray<char> charArray;
    CountArray<int> intArray;
    CountArray<std::string> strArray;

    template <typename T>
    static void print(CountArray<T> arr); // arr MUST be passed by value

    template <typename T>
    static void read(std::istream& is, CountArray<T>& arr);

public:
    Executive(std::istream& charFile, std::istream& intFile,
              std::istream& stringFile);

    void print() const;
};

然后是主文件:

#include <iostream>
#include <fstream>
#include "Executive.h"

//Get file names
int main(int argc, char* argv[]){

std::ifstream charFin, intFin, stringFin;

//open file, check if open, then continue if so.
charFin.open(argv[1]);
if(charFin.is_open()){
    argv[1].read(charFin, charArray);
}
else{
    std::cout <<"Unable to open " <<argv[1] <<std::endl;
    return -1;
}

intFin.open(argv[2]);
if(intFin.is_open()){
    argv[2].read(intFin, intArray);
}
else{
    std::cout <<"Unable to open " <<argv[2] <<std::endl;
    return -1;  
}

stringFin.open(argv[3]);
if(stringFin.is_open()){
    argv[3].read(stringFin, strArray);
}
else{
    std::cout <<"Unable to open " <<argv[3] <<std::endl;
    return -1;
}
Executive exec(charFin, intFin, stringFin);
exec.print();
return 0;
}

我不知道在哪里或如何修复这些错误,但我知道它们与模板有关,我知道这是一个非常狭隘的问题,但我希望它可以帮助其他有类似错误的人,因为他们都有同样的生根问题。

4

1 回答 1

0
template <typename T>
Count<T>::T(){
item=new Count<T>;
count=1;
}  

应该

template <typename T>
Count<T>::Count(){
item=new Count<T>;
count=1;
}

等等

于 2013-10-04T03:58:49.007 回答