codeblock编译C++出错error:expected initializer before✀<✀token

2025-03-12 19:33:49
推荐回答(1个)
回答1:

void Store :: PutElem 修改为void Store :: PutElem(T x)即可,使用"("而不是"<"。完整的正确代码如下:

#include
#include

using namespace std;

struct Student
{
int id;
float gpa;
};

template
class Store
{
private:
T item;
int haveValue;
public:
Store(void);
T GetElem(void);
void PutElem(T x);
};

template
Store ::Store(void) : haveValue(0){}

template
T Store ::GetElem(void)
{

if (haveValue == 0)
{
cout << "No item present !" << endl;
exit(1);
}
return item;
}

template
void Store ::PutElem(T x)
//此处是错误的地方《》《》《》《》《》《》《
{
haveValue++;
item = x;
}

int main(void)
{
Student g = { 1000, 23 };
Store s1, s2;
Store s3;
Store D;

s1.PutElem(3);
s2.PutElem(-7);
cout << s1.GetElem() << " " << s2.GetElem() << endl;

s3.PutElem(g);
cout << "This student id is " << s3.GetElem().id << endl;

cout << "Retrieving object D ";
cout << D.GetElem() << endl;

}