a little help for string used in classes

D

djm

hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.

//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H

#include <string>
using namespace std;
class Definition
{
public:
Definition(); //constructor(intialisation)
~Definition(); //destructor

//setter methods
void setDefinition(string);
void setNext(Definition*);

//getter Methods
string getDefinition();
Definition* getNext();

private:
string definition;
Definition* next;
};
#endif

************************************************************************************************************
//and the cpp file (Definition.cpp)
#include <iostream>
#include <string>
#include "Definition.h"
using namespace std;

Definition::Definition()
{
definition.clear();
next = NULL;
}

Definition::~Definition()
{
}

void Definition::setDefinition(string d)
{
definition = d;
}


void Definition::setNext(Definition* n)
{
next = n;
}

string Definition::getDefinition()
{
return definition; //*****line 29******
}

Definition* Definition::getDefinition()
{
return next; //*****line 34****
}

************************************************************************************************************
see the problem comes when i try to compile the Definition.cpp file. i
get the following erros

Definition.cpp:34: error: prototype for `Definition*
Definition::getDefinition()
' does not match any in class `Definition'
Definition.cpp:29: error: candidate is: std::string
Definition::getDefinition()
Definition.cpp:34: error: `Definition* Definition::getDefinition()'
and `std::st
ring Definition::getDefinition()' cannot be overloaded
*************************************************************************************************************

ive only done the linked list using integer data types, so i tried the
same coding for string. please forgive me if ive done any stupid
things in the coding.
please help me out
thanks!
 
J

Jim Langston

djm said:
hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.

//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H

#include <string>
using namespace std;
class Definition
{
public:
Definition(); //constructor(intialisation)
~Definition(); //destructor

//setter methods
void setDefinition(string);
void setNext(Definition*);

//getter Methods
string getDefinition();
Definition* getNext();

private:
string definition;
Definition* next;
};
#endif

************************************************************************************************************
//and the cpp file (Definition.cpp)
[SNIP]


string Definition::getDefinition()
{
return definition; //*****line 29******
}
Definition* Definition::getDefinition()
{
return next; //*****line 34****
}

You have 2 functions getDefinition both accepting no paramater. VC++
describes the problem a little differnent.

'Definition *Definition::getDefinition(void)' : overloaded function differs
only by return type from 'std::string Definition::getDefinition(void)'

You can't overload a functioion only be return type. The parameters have to
be different. So maybe rename the 2nd one to:
Definition* Definition::getNextDefinition()

then it would compile since you wouldn't be trying to overload the same
function name.
 
B

Barry

djm said:
hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.

//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H

#include <string>
using namespace std;
class Definition
{
public:
Definition(); //constructor(intialisation)
~Definition(); //destructor

//setter methods
void setDefinition(string);
void setNext(Definition*);

//getter Methods
string getDefinition();
Definition* getNext();

private:
string definition;
Definition* next;
};
#endif

************************************************************************************************************
//and the cpp file (Definition.cpp)
#include <iostream>
#include <string>
#include "Definition.h"
using namespace std;

Definition::Definition()
{
definition.clear();
next = NULL;
}

Definition::~Definition()
{
}

void Definition::setDefinition(string d)
{
definition = d;
}


void Definition::setNext(Definition* n)
{
next = n;
}

string Definition::getDefinition()
{
return definition; //*****line 29******
}

Definition* Definition::getDefinition()

Definition* Definition::getNext()
 
D

djm

hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.
//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H
#include <string>
using namespace std;
class Definition
{
public:
Definition(); //constructor(intialisation)
~Definition(); //destructor
//setter methods
void setDefinition(string);
void setNext(Definition*);
//getter Methods
string getDefinition();
Definition* getNext();
private:
string definition;
Definition* next;
};
#endif
************************************************************************************************************
//and the cpp file (Definition.cpp)
[SNIP]

string Definition::getDefinition()
{
return definition; //*****line 29******
}
Definition* Definition::getDefinition()
{
return next; //*****line 34****
}

You have 2 functions getDefinition both accepting no paramater. VC++
describes the problem a little differnent.

'Definition *Definition::getDefinition(void)' : overloaded function differs
only by return type from 'std::string Definition::getDefinition(void)'

You can't overload a functioion only be return type. The parameters have to
be different. So maybe rename the 2nd one to:
Definition* Definition::getNextDefinition()

then it would compile since you wouldn't be trying to overload the same
function name.

thanks Jim. i see the careless mistake ive done. now the program works
perfect..
thanks for your help. i really appreciate it.
 
G

Guest

hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.

//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H

#include <string>
using namespace std;

Never use "using namespace std;" in a header-file, it will come back and
bite you with hard to diagnose errors later since ny file that includes
this header will then be influenced by the using directive.
class Definition
{
public:
Definition(); //constructor(intialisation)
~Definition(); //destructor

//setter methods
void setDefinition(string);
void setNext(Definition*);

//getter Methods
string getDefinition();
Definition* getNext();

private:
string definition;
Definition* next;
};
#endif

************************************************************************************************************
//and the cpp file (Definition.cpp)
#include <iostream>
#include <string>
#include "Definition.h"
using namespace std;

Definition::Definition()
{
definition.clear();

You do not have to call clear, the string will be empty on creation.
next = NULL;
}

Definition::~Definition()
{

I do not know exactly how your liked list is supposed to work, but you
might want to have "delete next;" here to prevent any memory leaks.
}

void Definition::setDefinition(string d)
{
definition = d;
}


void Definition::setNext(Definition* n)
{
next = n;

What happens if next already points to some object? You might want to
"delete next;" before the assignment to prevent memory leaks.
}

string Definition::getDefinition()
{
return definition; //*****line 29******
}

Definition* Definition::getDefinition()

Definition* Definition::getNext()
 
D

Duane Hebert

djm said:
hello everyone, im doing a c++ coursework which consists linked lists
and use of classes. but im getting some compilation errors. can some
please help me out.

//this is the header file Definition.h
#ifndef DEFINTION_H
#define DEFINITON_H

#include <string>
using namespace std;

When you put this in a header file
everyone using your header is now using
namespace std. Sort of defeats the
purpose. You should lose points for that <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top