By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

a little help for string used in classes

P: n/a
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!

Oct 13 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"djm" <dj*******@gmail.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
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.
Oct 13 '07 #2

P: n/a
djm wrote:
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()
{
return next; //*****line 34****
}
Oct 13 '07 #3

P: n/a
djm
On Oct 13, 12:55 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"djm" <djmlog...@gmail.comwrote in message

news:11**********************@i13g2000prf.googlegr oups.com...
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.

Oct 13 '07 #4

P: n/a
djm
On Oct 13, 1:16 pm, Barry <dhb2...@gmail.comwrote:
djm wrote:
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()
{
return next; //*****line 34****
}
thanks for pointing out the mistake.
the program compiles without any errors now. :D

Oct 13 '07 #5

P: n/a
On 2007-10-13 09:23, djm wrote:
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()
{
return next; //*****line 34****
}
--
Erik Wikström
Oct 13 '07 #6

P: n/a

"djm" <dj*******@gmail.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
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>
Oct 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.