Sign in to follow this  

code trouble

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can anyone help me out with this linked list attempt, i originally thought it was way i set my classes, but majority of errors, based around how im returning values back. anyone explain, and correct what im doing wrong
#include <iostream>
#include <Windows.h>

int main() {
	int temp;
	std::cin>>temp;
	system("pause");
}

enum Result {
	IsSmaller= 0,
	IsBigger= 1,
	IsEqual= 2
};

class Data {
public:
	Data() { pData = rand(); }
	Data Compare(Data const &);
	int getData() { return pData; }
private:
	int pData;
}

Data::Compare(Data const & other) {
	if(other.pData > pData)
		return IsBigger;
	if(other.pData < pData)
		return IsSmaller;

	if(other.pData == pData)
		return IsEqual;
}
class Node :public Data {
	virtual void Show();
private:
	Data * MyData;
};

class LinkedList {
	LinkedList();
	~LinkedList() {}
};

class TailNode :public Node {
	TailNode();
};

TailNode::TailNode() {

}


class HeadNode :public Node {
public:
	HeadNode();
private:
	Node * MyNext;
};

HeadNode::HeadNode() {
	MyNext = TailNode * MyTail = new TailNode();
}

LinkedList::LinkedList() {
	HeadNode * MyHead = new HeadNode();
}



class Internal :public Node {
	Internal(Data const &, Node * next);
	void Insert(Data const *);

private:
	Data * MyData;
	Node * MyNext;
};

//What is the overloaders!!?!
//Compare.  Result = Functions.
Internal::Insert(Data & other) {	
	//WRONG HAS TO BE!
	switch(MyData->Compare(other.pData))
		case(IsSmaller):

		case(IsBigger):

		case(IsEqual):
}

Internal::Internal(Data const & other, Node * next) {
	MyData = other.getData;
	MyNext = next;
}


c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(31) : error C2664: 'Data::Data(const Data &)' : cannot convert parameter 1 from 'Result' to 'const Data &' Reason: cannot convert from 'Result' to 'const Data' No constructor could take the source type, or constructor overload resolution was ambiguous c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(31) : error C2553: no legal conversion of return value to return type 'Data' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(33) : error C2664: 'Data::Data(const Data &)' : cannot convert parameter 1 from 'Result' to 'const Data &' Reason: cannot convert from 'Result' to 'const Data' No constructor could take the source type, or constructor overload resolution was ambiguous c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(33) : error C2553: no legal conversion of return value to return type 'Data' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(36) : error C2664: 'Data::Data(const Data &)' : cannot convert parameter 1 from 'Result' to 'const Data &' Reason: cannot convert from 'Result' to 'const Data' No constructor could take the source type, or constructor overload resolution was ambiguous c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(36) : error C2553: no legal conversion of return value to return type 'Data' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(66) : error C2065: 'MyTail' : undeclared identifier c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(66) : error C2275: 'TailNode' : illegal use of this type as an expression c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(49) : see declaration of 'TailNode' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(66) : error C2248: 'TailNode::TailNode' : cannot access private member declared in class 'TailNode' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(50) : see declaration of 'TailNode::TailNode' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(49) : see declaration of 'TailNode' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(86) : error C2511: 'int Internal::Insert(Data &)' : overloaded member function not found in 'Internal' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(75) : see declaration of 'Internal' c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(97) : error C2475: 'Data::getData' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(97) : error C2440: '=' : cannot convert from 'int (__thiscall Data::* )(void)' to 'Data *' There is no context in which this conversion is possible

Share this post


Link to post
Share on other sites
1. You declared "Compare" as returning a "Data", it should be returning a "Result".

2. MyNext = TailNode * MyTail = new TailNode(); is a syntax error, since "TailNode * MyTail = new TailNode()" is not an lvalue. You can try:

MyNext = new TailNode();

3. The TailNode constructor is private, yet you attempt to call it from the HeadNode constructor

4. You define a function Insert( const Data & ) as being part of the class Internal, but never made mention of that function in the class definition.

5. Instead of the calling function getData, you take its address.

Share this post


Link to post
Share on other sites
thanks for teh reply, im still having troubles..show you here..

[source=c++] 
#include <iostream>
#include <Windows.h>


class TailNode;
class HeadNode;

int main() {
int temp;
std::cin>>temp;
system("pause");
}

enum {
IsSmaller= 0,
IsBigger= 1,
IsEqual= 2
};

class Data {
public:
Data() { pData = rand(); }
int Compare(Data const &);
int getData() { return pData; }
int pData;
};

int Data::Compare(Data const & other) {
if(other.pData > pData)
return IsBigger;
if(other.pData < pData)
return IsSmaller;

else
return IsEqual;
}

class Node {
public:
virtual void Show();
virtual Node * Insert(Data * theData)=0;
private:
Data * MyData;
};

class LinkedList {
public:
LinkedList();
~LinkedList() {;}
private:
HeadNode * MyHead;
};

class TailNode :public Node {
public:
TailNode();
};


TailNode::TailNode() { ;}

class HeadNode :public Node {
public:
HeadNode();
private:
Node * MyNext;
};

HeadNode::HeadNode() { MyNext = new TailNode(); }
LinkedList::LinkedList() { MyHead = new HeadNode(); }

class Internal :public Node {
public:
Internal(Data const &, Node * next);
virtual void Insert(Data const *);

private:
Data * MyData;
Node * MyNext;
};

//What is the overloaders!!?!
//Compare. Result = Functions.

void Internal::Insert(Data const * other) {
//WRONG HAS TO BE!
int x = MyData->Compare(*other);
switch(x) {
case(IsSmaller):
//Pass not MY cup of tea
MyNext->Insert(*other);
case(IsBigger):
Internal * dataNode = new Internal(*other,this);
case(IsEqual):
; //break
}
}


Internal::Internal(Data const & other, Node * next) {
MyData->pData = other.getData();
MyNext = next;
}










[Edited by - dcuk on August 31, 2005 5:15:16 AM]

Share this post


Link to post
Share on other sites
You have done simple mistakes -
1) Put a semicolon after the Data class ends.

class Data {
public:
Data() { pData = rand(); }
int Compare( Data const &);
int getData() { return pData; }
private:
int pData;
}; // <- This is missing semicolon

2) In following function make - changes -
Internal::Internal(Data const & other, Node * next) {
MyData->pData = other.getData();
MyNext = next;
}

replace the first line with following line.
MyData->pData = other.getData();

before doing this make sure that, in class Data, pData is made public.


Share this post


Link to post
Share on other sites
Thanks that helped alot, but theres still alot wrong with it to be honest.

c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(69) : error C2259: 'TailNode' : cannot instantiate abstract class
due to following members:
'Node *Node::Insert(Data *)' : pure virtual function was not defined
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(41) : see declaration of 'Node::Insert'
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(70) : error C2259: 'HeadNode' : cannot instantiate abstract class
due to following members:
'Node *Node::Insert(Data *)' : pure virtual function was not defined
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(41) : see declaration of 'Node::Insert'
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(91) : error C2664: 'Node::Insert' : cannot convert parameter 1 from 'const Data' to 'Data *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(93) : error C2259: 'Internal' : cannot instantiate abstract class
due to following members:
'Node *Node::Insert(Data *)' : pure virtual function was not defined
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(41) : see declaration of 'Node::Insert'
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(94) : error C2360: initialization of 'dataNode' is skipped by 'case' label
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(93) : see declaration of 'dataNode'
c:\Program Files\Microsoft Visual Studio\MyProjects\LinkedList\LinkedList.cpp(101) : error C2662: 'Data::getData' : cannot convert 'this' pointer from 'const Data' to 'Data &'
Conversion loses qualifiers


edited top source code

Share this post


Link to post
Share on other sites
You need break statements inside your switch.
A switch "falls through" to the bottom or the first break
statement after the first true case.

//In other words...
int x = 2;
switch(x) {
case 1: cout&lt;&lt;"1st Case ";
case 2: cout&lt;&lt;"2nd Case ";
case 3: cout&lt;&lt;"3rd Case ";
default: cout&lt;&lt;"NOT FOUND";
}
//Outputs this...
//2nd Case 3rd Case NOT FOUND
//

//What you want is this....
int x = 2;
switch(x) {
case 1: cout&lt;&lt;"1st Case ";
break;
case 2: cout&lt;&lt;"2nd Case ";
break;
case 3: cout&lt;&lt;"3rd Case ";
break;
default: cout&lt;&lt;"NOT FOUND";
}
//Which outputs this...
//2nd Case

Share this post


Link to post
Share on other sites
The answer you seek truly is right there in the error message. You need to define "Insert" methods for both the TailNode and HeadNode class since you derived both from the Node class (which contains a pure-virtual function "Insert"). A class that derives from an abstract class (i.e. a class that contains a pure-virtual function) is also an abstract class if it doesn't provide a definition for all of the pure-virtual functions in its base class. Your Node class has a pure-virtual function "Insert", HeadNode derives from Node, therefore, if you wish to instantiate an object of type HeadNode, you MUST provide a definition for the "Insert" method. This is also causing you some grief in your Internal class because the definition of the "Insert" function you give doesn't match what the abstract Node class requires.

Overall, I suspect the class structure you've given isn't what you intended to mean, so you might want to re-evaluate your design here. Why are you trying to treat head and tail nodes differently from a typical node? There really isn't a reason to declare them as independent types. There's nothing truly special about the first or last node that requires a whole new hierarchy.

Also, think about what it means to inherit from another class. The usual rule-of-thumb is to think of inheriting in the following way -

class A: public B
{};


implies A "is-a" B. In your code, you are essentially saying a Node "is-a" Data, which, again, I don't think you really mean. IMO, you should consider just having one plain non-derived Node class and have it contain a pointer to the next Node and contain a pointer to a Data object...DON'T derive from Data. Containment usually implies a "has-a" relationship. In the revised case, a Node "has-a" Data element associated with it and "has-a" pointer to the next Node in the list.




Share this post


Link to post
Share on other sites

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this