簡體   English   中英

使用模板時,如何解決以下編譯器錯誤,以實現程序所需的行為?

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

我正在從一本寫得不太好的C ++書中練習Visual Studio 2013中的模板,並且分別收到以下編譯器錯誤:“錯誤C4430:缺少類型說明符-假定為int。注意:C ++不支持default-int”“錯誤C2143:語法錯誤:在'<'“之前缺少','

兩者都將我引至第10行,即...

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

我的代碼的期望行為是顯示使用模板創建和破壞臨時Animal對象的行為。

到目前為止,我嘗試過的故障排除步驟包括在返回類型和第10行的第一個參數類型中將std :: iostream&替換為int 。這樣做之后,錯誤消息仍然相同。 這似乎表明問題可能是第二個參數類型。 然后,我在第10行的重載運算符函數的第二個參數(operator <<)中添加了關鍵字typename ,並且錯誤仍然存​​在。

第10行可以在下面的頭文件中找到。

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

這是本書中其余的代碼,供您參考。

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

 #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

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

我盡力為您提供程序的期望行為,特定問題或錯誤以及重現該程序所需的最短代碼,以使該問題始終處於主題中。 所有這些都是調試幫助的要求,如“ 幫助中心”頁面中所述。 我是該網站的新手,如果我的問題仍然很不在意,請讓我知道如何更改我的問題以更多地關注主題,或者告訴我一個更好的地方來提問。 - 謝謝

在聲明Array類之前就聲明了此函數(即,編譯器從未見過該類型,也不知道它是什么,因此會出錯)。

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

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

編輯

我剛才注意到, operator<<是之前聲明Array類,並為中友元函數Array類。 您可以只刪除頂部的聲明。

編輯2

數組

我刪除了以下項目

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

將朋友功能聲明更新為

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>&);

動物

此類具有friend函數,但未在cpp文件中實現。 我將定義從main.cpp移到Animal.cpp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM