gpt4 book ai didi

c++ - 堆栈数据结构作为模板 : Main method do not identify methods in stack

转载 作者:太空宇宙 更新时间:2023-11-04 15:47:09 24 4
gpt4 key购买 nike

请看下面的代码,

堆栈.h

template <typename T>

class Stack
{
public:
Stack(int number)
{
maxSize = number;
top = -1;
stackData = new T*[maxSize];
}

~Stack()
{
delete [] stackData;
}

int count()
{

}

bool isEmpty()
{
if(top==-1)
{
return true;
}
else
{
return false;
}
}

bool isFull()
{
if(top== (maxSize-1))
{
return true;
}
else
{
return false;
}
}

*T pop()
{
if(!isEmpty())
{
return stackData[top--]; // Remove Item From Stack
}
}

*T peek()
{
T *peekData = &stackData[top];
return peekData;
}

void push(T *pushValue)
{
if(!isFull())
{
stackData[++top] = pushValue;
}
}

private:
int maxSize;
T ** stackData;
int top;
};

main.cpp

#include <iostream>
#include "Stack.h"
#include <iostream>

using namespace std;

int main()
{
int i = 0;

Stack<double> doubleStack(5);
double doubleValue = 1.1;

cout << "pushing elements into the stack" << endl;

while(i<5)
{
doubleStack.push();
}

system("pause");
return 0;
}

当我运行这段代码时,出现以下错误。

1>------ Build started: Project: CourseWork2, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(74) : see reference to class template instantiation 'Stack<T>' being compiled
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(54): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(60): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1> c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(11) : see reference to class template instantiation 'Stack<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(18): error C2660: 'Stack<T>::push' : function does not take 0 arguments
1> with
1> [
1> T=double
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Intellelisense 不识别除 isFull()、count() 和 isEmpty() 之外的任何方法。因此,我无法编写其余代码!

这是为什么?请帮忙!

最佳答案

你把 * 放在函数语法中的错误位置:

更新:

 *T pop()
*T peek()

收件人:

 T* pop()
T* peek()

关于c++ - 堆栈数据结构作为模板 : Main method do not identify methods in stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766528/

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