LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2003, 02:39 PM   #1
ReverseLogic
LQ Newbie
 
Registered: Aug 2001
Posts: 9

Rep: Reputation: 0
Strange Errors in C++


I am getting some odd errors when I try to compile my code. Both the header and source files are in the same directory. BTW, if anyone wants to use the code, they are welcome, excluding for commercial purpose.

this is a windows based program using MSVC++ 6

//In linkedlist.h

#ifndef LINKEDLIST_H
#define LINKEDLIST_H


class CLinkedList
{
public:
void CLinkedList();


void AddNode(CNode *pNode);
CNode * FindNodeByIndex(int iIndex);
int GetNodeIndex(CNode *pNode);
void RemoveNode(CNode *pNode);
void RemoveNode(int iIndex);
int GetLastIndex();
CNode * GetLastNode();


~CLinkedList();

private:
CNode * m_pFirstNode;

};



//contained by CLinkedList class
// Other classes will inherit from this.
// No functionality by itself
class CNode
{
public:
void CNode(); //Default Constructor

CNode GetNextNode() {return m_pNextNode;}
void SetNextNode(CNode* pNextNode) { m_pNextNode = pNextNode;}

void SetIndex(int iIndex) {m_iIndex = iIndex;}
int GetIndex() {return m_iIndex;}


~CNode(); //deletes the pointer to the next item

private:

CNode * m_pNextNode;
int m_iIndex;

};

#endif // inclusion gaurd

//**********************
//linkedlist.cpp
//**********************

#include "LinkedList.h"
#include "stdafx.h"


CLinkedList::CLinkedList()
{
m_pFirstNode = NULL;

}

CLinkedList::~CLinkedList()
{
m_pFirstNode->~CNode(); //this makes sure the memory is returned

delete m_pFirstNode;
}

void CLinkedList::AddNode(CNode * pNode)
{
if(m_pFirstNode != NULL)
{
GetLastNode()->SetNextNode(pNode);
pNode->SetIndex(GetLastIndex() + 1);
}
else
{
m_pFirstNode = pNode;
m_pFirstNode = 0;
}

}

CNode * CLinkedList::FindNodeByIndex(int iIndex)
{
if (iIndex > GetLastIndex())
{
return NULL;
}

int iCurrentIndex = 0;
CNode * pNode = m_pFirstNode;
while ( iCurrentIndex < GetLastIndex() )
{
if (iCurrentIndex == iIndex)
{
return pNode;
}

pNode = pNode->GetNextNode();
iCurrentIndex++;
}

}

int CLinkedList::GetNodeIndex(CNode* pNode)
{

int iCurrentIndex = 0;
CNode * pCurrentNode = m_pFirstNode;
while ( iCurrentIndex < GetLastIndex() )
{
if (pCurrentNode == pNode)
{
return pNode;
}

pCurrentNode = pCurrentNode->GetNextNode();
iCurrentIndex++;
}


}

void CLinkedList::RemoveNode(CNode * pNode)
{
FindNodeByIndex(pNode->GetIndex - 1)->SetNextNode(pNode->GetNextNode());

pNode->SetNextNode(NULL);
~CNode();
}

void CLinkedList::RemoveNode(int iIndex)
{
FindNodeByIndex(iIndex-1)->SetNextNode(FindNodeByIndex(iIndex)->GetNextNode();

FindNodeByIndex(iIndex)->SetNextNode(NULL);
~CNode();

}

int CLinkedList::GetLastIndex()
{
int iCurrentIndex = 0;
CNode * pNode = m_pFirstNode;
while ( pNode->GetNextNode != NULL )
{

pNode = pNode->GetNextNode();
iCurrentIndex++;
}
return iCurrentIndex;

}

CNode * CLinkedList::GetLastNode()
{
int iCurrentIndex = 0;
CNode * pNode = m_pFirstNode;
while ( pNode->GetNextNode != NULL )
{

pNode = pNode->GetNextNode();
iCurrentIndex++;
}

return pNode;

}





CNode::CNode()
{
m_pNextNode = NULL;
}


CNode::~CNode()
{
if (m_pNextNode != NULL);
{
m_pNextNode->~CNode();
}
delete m_pNextNode;
}

//***********************
//Error messages
//***********************

C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(5) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(7) : error C2065: 'm_pFirstNode' : undeclared identifier
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(9) : warning C4508: 'CLinkedList' : function should return a value; 'void' return type assumed
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(11) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(12) : error C2084: function 'int __cdecl CLinkedList(void)' already has a body
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(15) : error C2541: delete : cannot delete objects that are not pointers
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(18) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(18) : error C2065: 'CNode' : undeclared identifier
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(18) : error C2065: 'pNode' : undeclared identifier
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(19) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(33) : error C2143: syntax error : missing ';' before '*'
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(33) : error C2501: 'CNode' : missing storage-class or type specifiers
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(33) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(34) : error C2501: 'FindNodeByIndex' : missing storage-class or type specifiers
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(35) : error C2065: 'GetLastIndex' : undeclared identifier
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(41) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(49) : error C2227: left of '->GetNextNode' must point to class/struct/union
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(55) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(56) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(74) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(75) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(82) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(84) : error C2227: left of '->SetNextNode' must point to class/struct/union
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(84) : error C2227: left of '->GetNextNode' must point to class/struct/union
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(84) : error C2143: syntax error : missing ')' before ';'
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(86) : error C2227: left of '->SetNextNode' must point to class/struct/union
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(87) : warning C4552: '~' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(91) : error C2653: 'CLinkedList' : is not a class or namespace name
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(92) : error C2373: 'GetLastIndex' : redefinition; different type modifiers
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(94) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\RRH324_USER\My Documents\RP\Common Files\LinkedList.cpp(95) : error C2227: left of '->GetNextNode' must point to class/struct/union
 
Old 01-20-2003, 12:35 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
Can't say your errors are strange.

You have a lot of compile and logical problems with you program. Most of the problems I found so far are basic C++ problems. For starters you need to declare your CNode before CLinkedList.

I'd suggest gutting out all the member functions and start small. Try to compile the program with just the class Node, then members, then add the LinkedList class; add a members. Compile after each small modification.
 
Old 01-20-2003, 03:06 AM   #3
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
Quote:
void CLinkedList();
also, constructors can't have a return type. it's the same thing as your destructor without the "~" in your class definition.

Last edited by megaspaz; 01-20-2003 at 03:08 AM.
 
Old 01-21-2003, 12:56 AM   #4
ReverseLogic
LQ Newbie
 
Registered: Aug 2001
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for the advice. I will do that from now on. I took all the code from LinkedList.cpp, and pasted it at the end of the header file, and it worked for some reason. Any ideas on why it wouldn't work in the source file?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
very strange aptitude errors ronkymac Linux - Software 6 11-23-2005 05:12 PM
strange SMART errors sensovision Linux - Hardware 2 03-02-2005 06:43 PM
Strange pci errors Neruocomp Slackware 0 01-07-2005 11:32 AM
strange errors eltn Programming 2 10-29-2004 10:43 PM
Strange errors XPediTioN Slackware 1 07-11-2003 06:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration