简体   繁体   English

使用模板时,如何解决以下编译器错误,以实现程序所需的行为?

[英]When using templates, how do I fix the following compiler error to achieve the desired behavior of my program?

I'm practicing templates in Visual Studio 2013 from a not so well written C++ book and I receive the following compiler errors respectively, "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int" and "error C2143: syntax error : missing ',' before '<'" . 我正在从一本写得不太好的C ++书中练习Visual Studio 2013中的模板,并且分别收到以下编译器错误:“错误C4430:缺少类型说明符-假定为int。注意:C ++不支持default-int”“错误C2143:语法错误:在'<'“之前缺少','

Both refer me to line 10 which is... 两者都将我引至第10行,即...

std::ostream& operator<< (std::ostream&, const Array<T>&);

The desired behavior of my code is to show the creation and destruction of temporary Animal objects using templates. 我的代码的期望行为是显示使用模板创建和破坏临时Animal对象的行为。

The troubleshooting steps I have tried so far include substituting std::iostream& with int in my return type and first parameter type of line 10. After doing so the error messages remained the same. 到目前为止,我尝试过的故障排除步骤包括在返回类型和第10行的第一个参数类型中将std :: iostream&替换为int 。这样做之后,错误消息仍然相同。 This seemed to suggest that the problem may be the second parameter type. 这似乎表明问题可能是第二个参数类型。 I then added the keyword typename to the second parameter of my overloading operator function (operator<<) on line 10 and the error still persisted. 然后,我在第10行的重载运算符函数的第二个参数(operator <<)中添加了关键字typename ,并且错误仍然存​​在。

Line 10 can be found in the header file below. 第10行可以在下面的头文件中找到。

//Array.h //Array.h

 #ifndef ARRAY_H #define ARRAY_H #include <iostream> #include "Animal.h" const int DefaultSize = 3; template <typename T> std::ostream& operator<< (std::ostream&, const Array<T>&); template <typename T> // declare the template and the paramenter class Array // the class being parameterized { public: Array(int itsSize = DefaultSize); Array(const Array &rhs); ~Array() { delete[] pType; } // operators Array& operator=(const Array&); T& operator[](int offSet) { return pType[offSet]; } const T& operator[](int offSet) const { return pType[offSet]; } // accessors int GetSize() const { return itsSize; } // friend function friend std::ostream& operator<< (std::ostream&, const Array<T>&); private: T *pType; int itsSize; }; template <typename T> Array<T>::Array(int size = DefaultSize) :itsSize(size) { pType = new T[size]; for (int i = 0; i < size; i++) pType[i] = static_cast<T>(0); } Array<Animal>::Array(int AnimalArraySize) :itsSize(AnimalArraySize) { pType = new Animal[AnimalArraySize]; } template <typename T> Array<T>::Array(const Array &rhs) { itsSize = rhs.GetSzie(); pType = new T[itsSize]; for (int i = 0; i < itsSize; i++) pType[i] = rhs[i]; } template <typename T> Array<T>& Array<T>::operator=(const Array &rhs) { if (this == &rhs) return *this; delete[] pType; itsSize = rhs.GetSize(); pType = new T[itsSize]; for (int i = 0; i < itsSize; i++) pType[i] = rhs[i]; return *this; } template <typename T> std::ostream& operator<< (std::ostream& output, const Array<T> &theArray) { for (int i = 0; i < theArray.GetSize(); i++) output << "[" << i << "]" << theArray[i] << std::endl; return output; } #endif 

Here's the rest of my code from my book for your reference. 这是本书中其余的代码,供您参考。

//Animal.h //Animal.h

 #ifndef ANIMAL_H #define ANIMAL_H #include <iostream> class Animal { public: // constructors Animal(); Animal(int); ~Animal(); // accessors int GetWeight() const { return itsWeight; } void SetWeight(int theWeight) { itsWeight = theWeight; } // friend operators friend std::ostream& operator<<(std::ostream&, const Animal&); private: int itsWeight; }; #endif 

//Animal.cpp //Animal.cpp

 #include "Animal.h" #include <iostream> Animal::Animal() :itsWeight(0) { std::cout << "animal() "; } Animal::Animal(int weight) : itsWeight(weight) { std::cout << "animal(int) "; } Animal::~Animal() { std::cout << "Destroyed an animal..."; } 

//Main.cpp //Main.cpp

 #include <iostream> #include "Animal.h" #include "Array.h" std::ostream& operator<<(std::ostream&, const Animal&); void IntFillFunction(Array<int>& theArray); void AnimalFillFunction(Array<Animal>& theArray); int main() { Array<int> intArray; Array<Animal> animalArray; IntFillFunction(intArray); AnimalFillFunction(animalArray); std::cout << "intArray...\\n" << intArray; std::cout << "\\nanimalArray...\\n" << animalArray << std::endl; std::cin.get(); return 0; } std::ostream& operator<<(std::ostream& theStream, const Animal& theAnimal) { theStream << theAnimal.GetWeight(); return theStream; } void IntFillFunction(Array<int>& theArray) { bool Stop = false; int offset, value; while (!Stop) { std::cout << "Enter an offset (0-9) and a value. "; std::cout << "(-1 to stop): "; std::cin >> offset >> value; if (offset < 0) break; if (offset > 9) { std::cout << "***Please use values between 0 and 9.***\\n"; continue; } theArray[offset] = value; } } void AnimalFillFunction(Array<Animal>& theArray) { Animal *pAnimal; for (int i = 0; i < theArray.GetSize(); i++) { pAnimal = new Animal(i * 10); theArray[i] = *pAnimal; delete pAnimal; } } 

I've tried my best to keep this question on topic by providing you with the desired behavior of the program, a specific problem or error and the shortest code necessary to reproduce it. 我尽力为您提供程序的期望行为,特定问题或错误以及重现该程序所需的最短代码,以使该问题始终处于主题中。 All of which are requirements for debugging help as documented in the Help Center page. 所有这些都是调试帮助的要求,如“ 帮助中心”页面中所述。 I'm new to this website, and if my question is still off-topic, please let me know how I can change my question to be more on topic or inform me of a better place to ask my question. 我是该网站的新手,如果我的问题仍然很不在意,请让我知道如何更改我的问题以更多地关注主题,或者告诉我一个更好的地方来提问。 - Thank you - 谢谢

This function is declared before the Array class is declared (ie, the compiler has never seen that type and doesn't know what it is, hence your error). 在声明Array类之前就声明了此函数(即,编译器从未见过该类型,也不知道它是什么,因此会出错)。

template<typename> class Array; // Add this forward declaration

template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);

Edit 编辑

I just noticed that the operator<< is declared before the Array class and as a friend function within the Array class. 我刚才注意到, operator<<是之前声明Array类,并为中友元函数Array类。 You could just remove the declaration at the top. 您可以只删除顶部的声明。

Edit 2 编辑2

Array.h 数组

I removed the following items 我删除了以下项目

#include "Animal.h"

template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);

Array<Animal>::Array(int AnimalArraySize) :itsSize(AnimalArraySize)
{
  pType = new Animal[AnimalArraySize];
}

Updated the friend function declartion to be 将朋友功能声明更新为

template<typename T> // This is not a member function so must be declared as a template
friend std::ostream& operator<< (std::ostream&, const Array<T>&);

Animal.h 动物

This class has the friend function but it's not implemented in the cpp file. 此类具有friend函数,但未在cpp文件中实现。 I moved the definition from the main.cpp into Animal.cpp 我将定义从main.cpp移到Animal.cpp

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

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