c++ - Count.h :10:5: error: ISO C++ forbids declaration of 'T' with no type [-fpermissive] T(); ^

标签 c++ templates compilation compiler-errors

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

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

具有执行官调用的实例:
#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;
}

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

最佳答案

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

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

等等

关于c++ - Count.h :10:5: error: ISO C++ forbids declaration of 'T' with no type [-fpermissive] T(); ^,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19172914/

相关文章:

C++ 类声明和命名空间

java - 使用 Java 开发可重用的模板项目

c++ - 带有模板 C++ 的嵌套类

c++ - 编译安装很简单

c++ - CMake 对 main 的 undefined reference

C++ CreateProcess - 系统错误 #2 找不到文件 - 我的文件路径有什么问题?

c# - 将 "Treat wchar_t as built-in type"设置为 No 会影响 C# 互操作吗?

c++ - 将 std::stringstream 数据高效解析为 std::vector<std::vector<double>>

c++ - 如何将 Bullet 物理库添加到 C++ 程序中

c++ - 在 C++ 标准中的什么地方可以找到对在全局范围内调用函数的支持?