gpt4 book ai didi

c++ - 在 C++ 中声明模板

转载 作者:行者123 更新时间:2023-11-28 02:33:44 25 4
gpt4 key购买 nike

// stdafx.h 

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//


#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#include "Animal.h"

// TODO: reference additional headers your program requires here

class Animal
{
private:
int itsWeight;
public:
Animal(int);
Animal();
~Animal() {}
int getWeight() const { return itsWeight; }
void Display() const;
};


template <class T>
class Array
{
private:
T *pType;
int itsSize;
const int defaultSize = 10;
public:

//constructors
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]; }

//methods of Access
int getSize() const { return itsSize; }
};

//constructor
template <class T>
Array<T>::Array(int size) :
itsSize(size)
{
pType = new T[size];
for (int i = 0; i < size; i++)
{
pType[i] = 0;
}
}

//copy-constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.getSize();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
{
pType[i] = rhs[i];
}
}

//operator prisvoeniya
template <class 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;
}

//this is the file "Animal.cpp"

#include "stdafx.h"
#include "Animal.h"

Animal::Animal()
{
itsWeight = 0;
}

Animal::Animal(int weight)
{
itsWeight = weight;
}

void Animal::Display() const
{
cout << itsWeight;
}
// the main function

#include "stdafx.h"

int_tmain(int argc, _TCHAR* argv[])
{

Array<int> theArray; //Integer array
Array<Animal> theZoo; //Animal array
Animal *pAnimal;

//filling the array
for (int i = 0; i < theArray.getSize(); i++)
{
theArray[i] = i * 2;
pAnimal = new Animal[i * 3];
theZoo[i] = *pAnimal;
delete pAnimal;
}

for (int j = 0; j < theArray.getSize(); j++)
{
cout << "theArray[" << j << "]:\t";
cout << theArray[j]<<"\t\t";
cout << "theZoo[" << j << "]:\t";
theZoo[j].Display();
cout << endl;
}

return 0;
}

问题是:编译器给我错误

Error 1 error C2648: 'Array<int>::defaultSize' : use of member as default parameter requires static member
d:\documents\work\c++ files\tigrans\homework10\templates\templates\templates\animal.h 28 1 Templates

Error 2 error C2648: 'Array<Animal>::defaultSize' : use of member as default parameter requires static member
d:\documents\work\c++ files\tigrans\homework10\templates\templates\templates\animal.h 28 1 Templates

任何人都可以帮助我理解这一点。我改变了

const int defaultSize=10;

static const int defaultSize=10 

然后没有错误,但在那个时候显示Debug Assertion Failed!

最佳答案

你的这部分代码很狡猾

{
pAnimal = new Animal[i * 3];
theZoo[i] = *pAnimal;
delete pAnimal;
}

第一行分配一个 i*3 Animal 数组,使用它们的默认构造函数(它使 Animal它的权重=0)。在第二行中,您将这些新分配的第一个 Animal 分配给 theZoo[i]。最后,第三行尝试取消分配 Animal

最后一行包含错误,因为您对通过 new [] 获得的指针调用了 delete

在堆上创建对象只是为了立即销毁它们的整个概念是相当可疑的——也许你来自另一种编程语言,这是创建对象的唯一方法?首先,您可以简单地使用一个自动变量

{
Animal a; // or a(i*3);
theZoo[i] = a;
}

或者更简短

{
theZoo[i] = Animal(i*3);
}

(请注意,如果您要使用 std 容器,您可以说 theZoo.emplace_back(i*3);,避免复制 Animal.)

关于c++ - 在 C++ 中声明模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28252767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com