需要帮助解决一些错误消息
我已经明白了这一点,并且已经在几个地方查看了已经尝试解决第一个错误的问题,但我无法将解决方案应用于它,所以我希望这里会有所帮助。
(84): error c4430: missing type specifier - int assumed. Note: C++ does not support default-int
(84): warning C4346: 'SortedLinkedList<T>::{ctor}' : dependent name is not a type
(84): error C2143: syntax error : missing ',' before '&'
(93): error C2244: 'SortedLinkedList<T>::{ctor}' : unable to match function definition to an existing declaration
(125): error C2244: 'SortedLinkedList<T>::add' : unable to match function definition to an existing declaration
(42) : see declaration of 'SortedLinkedList<T>::add'
(150): error C2244: 'SortedLinkedList<T>::toString' : unable to match function definition to an existing declaration
(48) : see declaration of 'SortedLinkedList<T>::toString'
#ifndef SORTEDLINKEDLIST_H
#define SORTEDLINKEDLIST_H
#include <iostream>
#include <new> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
using namespace std;
//**********************
// Struct class *
//**********************
template <class T>
struct Node {
int data;
Node* next;
};
template <class T>
class SortedLinkedList{
private:
T* head;
T* node1;
T* node2;
T* node;
T* n;
T* ptr;
int* size;
public:
//constructor
SortedLinkedList();
//copy constructor
SortedLinkedList(const SortedLinkedList&);
//destructor
~SortedLinkedList();
//destroy function. Calls the destructor.
T destroy();
//add function.
(42)T add (const int value);
// boolean accessor
bool exists(int element);
// toString accessor
(43) T toString();
T&运算符<< (const int &); };
//***************************************************
// Constructor. Sets the head to null and size to 0 *
//***************************************************
template <class T>
SortedLinkedList<T>::SortedLinkedList(){
head = NULL;
size = 0;
}
//***********************************************
// Destructor. Creates a temp pointer and moves *
// the head over till head is NULL *
//***********************************************
template <class T>
SortedLinkedList<T>::~SortedLinkedList(){
while (head != NULL) {
ptr = head;
head = head -> next;
delete ptr;
}
cout << "\nBut oh well...DESTRUCTED!\n" << endl;
}
//***********************************************
// My Copy Constructor *
//***********************************************
template <class T>
(84)
SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt, &obj){
size = obj.size;
head = obj.head;
node = obj.node;
node1 = obj.node1;
node2 = obj.node2;
n = obj.node;
cout << "COPIED!" << endl;
(93) }
//*************************************************
// And here is where all the fun begins. *
// This checks the element and rearranges the list*
// to its appropriateness *
//*************************************************
template <class T>
void SortedLinkedList<T>::add(int newElement){
if (head == NULL){
head = new Node;
head->next = NULL;
head->data = (newElement);
}
else if(head->data > newElement){
node1 = new Node;
node1->data = newElement;
node1->next = head;
head = node1;
}
else{
for(node2=head; node2->next!= NULL; node2 = node2->next)
if(node2->next->data > newElement)
break;
node = new Node;
node->next = (node2->next);
node->data = (newElement);
node2->next = (node);
++size;
}
(125)
}
//***********************************************
// Checks to see if inputed number exist in List*
//***********************************************
template <class T>
bool SortedLinkedList<T>::exists (int element){
for (n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
if(element == n->data) //analogous to compareTo (java)
return true;
return false;
}
//***********************************************
// toString method outputs the whole vector list*
//***********************************************
template <class T>
void SortedLinkedList<T>::toString(){
for (n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
(150) }
#endif
I've gotten up this point and I've looked in couple of places already trying to just solve the first error, but I'm not able to apply the solutions to it, so I'm hoping here will help.
(84): error c4430: missing type specifier - int assumed. Note: C++ does not support default-int
(84): warning C4346: 'SortedLinkedList<T>::{ctor}' : dependent name is not a type
(84): error C2143: syntax error : missing ',' before '&'
(93): error C2244: 'SortedLinkedList<T>::{ctor}' : unable to match function definition to an existing declaration
(125): error C2244: 'SortedLinkedList<T>::add' : unable to match function definition to an existing declaration
(42) : see declaration of 'SortedLinkedList<T>::add'
(150): error C2244: 'SortedLinkedList<T>::toString' : unable to match function definition to an existing declaration
(48) : see declaration of 'SortedLinkedList<T>::toString'
#ifndef SORTEDLINKEDLIST_H
#define SORTEDLINKEDLIST_H
#include <iostream>
#include <new> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
using namespace std;
//**********************
// Struct class *
//**********************
template <class T>
struct Node {
int data;
Node* next;
};
template <class T>
class SortedLinkedList{
private:
T* head;
T* node1;
T* node2;
T* node;
T* n;
T* ptr;
int* size;
public:
//constructor
SortedLinkedList();
//copy constructor
SortedLinkedList(const SortedLinkedList&);
//destructor
~SortedLinkedList();
//destroy function. Calls the destructor.
T destroy();
//add function.
(42)T add (const int value);
// boolean accessor
bool exists(int element);
// toString accessor
(43) T toString();
T &operator << (const int &);
};
//***************************************************
// Constructor. Sets the head to null and size to 0 *
//***************************************************
template <class T>
SortedLinkedList<T>::SortedLinkedList(){
head = NULL;
size = 0;
}
//***********************************************
// Destructor. Creates a temp pointer and moves *
// the head over till head is NULL *
//***********************************************
template <class T>
SortedLinkedList<T>::~SortedLinkedList(){
while (head != NULL) {
ptr = head;
head = head -> next;
delete ptr;
}
cout << "\nBut oh well...DESTRUCTED!\n" << endl;
}
//***********************************************
// My Copy Constructor *
//***********************************************
template <class T>
(84)
SortedLinkedList<T>::SortedLinkedList(const SortedLinkedListInt, &obj){
size = obj.size;
head = obj.head;
node = obj.node;
node1 = obj.node1;
node2 = obj.node2;
n = obj.node;
cout << "COPIED!" << endl;
(93) }
//*************************************************
// And here is where all the fun begins. *
// This checks the element and rearranges the list*
// to its appropriateness *
//*************************************************
template <class T>
void SortedLinkedList<T>::add(int newElement){
if (head == NULL){
head = new Node;
head->next = NULL;
head->data = (newElement);
}
else if(head->data > newElement){
node1 = new Node;
node1->data = newElement;
node1->next = head;
head = node1;
}
else{
for(node2=head; node2->next!= NULL; node2 = node2->next)
if(node2->next->data > newElement)
break;
node = new Node;
node->next = (node2->next);
node->data = (newElement);
node2->next = (node);
++size;
}
(125)
}
//***********************************************
// Checks to see if inputed number exist in List*
//***********************************************
template <class T>
bool SortedLinkedList<T>::exists (int element){
for (n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
if(element == n->data) //analogous to compareTo (java)
return true;
return false;
}
//***********************************************
// toString method outputs the whole vector list*
//***********************************************
template <class T>
void SortedLinkedList<T>::toString(){
for (n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
(150) }
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(84)
那个“,”在那里做什么?
应该是:
还:
必须是:
不像代码中的
void
。或者在声明中使用
void
但void toString();
。定义必须与声明匹配。(84)
What is that ',' doing there?
Should be:
ALSO:
Must be:
Not
void
like in your code.Or
void
butvoid toString();
in declaration either. Definition must match declaration.请注意,但我认为您的复制构造函数应该采用 const SortedLinkedListInt 而不是 const SortedLinkedListInt
此外,您的 add 方法不遵循您的原型(返回 void 而不是 T )
Note sure, but I think your copy constructor should take a const SortedLinkedListInt instead of a const SortedLinkedListInt
Moreover, your add method does not follow your prototype (returns void instead of T)
也许我错过了一些东西,但是:
Maybe I miss something, but: