嗨,我仍然想知道为什么我收到此错误消息:
使用类模板'Array'需要模板参数
标题:
#ifndef Array_h
#define Array_h
template< typename T>
class Array
{
public:
Array(int = 5);
Array( const Array &);
const Array &operator=(Array &);
T &operator[](int);
T operator[](int) const;
// getters and setters
int getSize() const;
void setSize(int);
~Array();
private:
int size;
T *ptr;
bool checkRange(int);
};
#endif
Run Code Online (Sandbox Code Playgroud)
CPP文件
template< typename T >
const Array &Array< T >::operator=(Array &other)
{
if( &other != this)
{
if( other.getSize != this->size)
{
delete [] ptr;
size = other.getSize;
ptr = new …Run Code Online (Sandbox Code Playgroud) 我仍然想知道为什么在尝试声明向量时会出现错误消息:
"未知的类型名称'setSize'"
#ifndef INTEGERSET_H
#define INTEGERSET_H
#include <vector>
using namespace std;
class IntegerSet
{
public:
IntegerSet();
IntegerSet(const int [], const int);
IntegerSet & unionOfSets(const IntegerSet &, const IntegerSet &) const;
IntegerSet & intersectionOfSets(const IntegerSet &, const IntegerSet &) const;
void insertElement(int);
void deleteElement(int);
void printSet();
bool isEqualTo(const IntegerSet &);
const int setSize = 10;
vector<bool> set(setSize);
};
#endif
Run Code Online (Sandbox Code Playgroud)
PS:我必须在每一行添加4个空格才能复制和粘贴上面的代码,因为它们都没有格式化.有没有更简单的方法?
创建基本类型的变量时,例如:
double *aPtr = new double;
Run Code Online (Sandbox Code Playgroud)
*(aPtr)不会被初始化.
有没有办法让它初始化为特定值(不一定是0),例如-1?
创造新课程的唯一途径是什么?
打印数组时,初始化整数有效.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i <= (MAX_SIZE - 1); i++)
{
printf("%3d",a[i]);
}
Run Code Online (Sandbox Code Playgroud)
但是,我想知道为什么初始化一个指向整数("walker")的指针不起作用:
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; aWalk <= aEnd; aWalk++)
{
printf("%3d", *aWalk);
}
Run Code Online (Sandbox Code Playgroud)