ID:182338
 
I'm implementing several data structures to toy around with C++ (linked lists, deques, queues, and stacks), and I'm working with nodes for these right now. This is the node portion of my header file:
class node
{
private:
int* value;
int* before, after;

public:
int Read();
bool Write(int);

int* Before();
int* After();

bool SetBefore();
bool SetAfter();
};

Now, when I try and do either of these, they get the error that follows it:
#include "header.h"

//Implementation of the node class.
int node::Read()
{
return 1;
}

/*
Compiling: main.cpp
Compiling: Structures.cpp
Linking console executable: bin\Release\Data Structures Thing.exe
obj\Release\Structures.o:Structures.cpp:(.text+0x0): multiple definition of `node::Read()'
obj\Release\main.o:main.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
*/

#include "header.h"

//Implementation of the node class.
node::Read()
{
return 1;
}

/*
error: ISO C++ forbids declaration of `Read' with no type|
||=== Build finished: 1 errors, 0 warnings ===|
*/

Looking in the eBook I have, the first one should be correct, but I get a redefinition error. Any help? =/
Holy crap! You changed your avatar and I could not recognize you!

But anyways...

You forgot conditional compilation to prevent headers from being included more than once. Also, you forgot the return value type in the implementation of node::Read on the second one.

George Gough
In response to KodeNerd
KodeNerd wrote:
Holy crap! You changed your avatar and I could not recognize you!

It's only temporary, and there's a reason.

You forgot conditional compilation to prevent headers from being included more than once.

They're there, but I didn't show them, as the whole header file is long.

Also, you forgot the return value type in the implementation of node::Read on the second one.

Yes, I know. In the afterbit of the code, I stated that I didn't think the latter one was wrong. I just don't know why the first one is right.
In response to Popisfizzy
I am going to think about this for a little bit but I am left wondering why you are implementing your own when there are plenty of pre-made structures such as this. Not to mention the use of raw pointers is not a good idea, try using boost::shared_ptr to prevent any memory leaks that I can already see in this implementation.

George Gough

[Edit]
As it states, you must have redefined int node::Read() somewhere else. Double check you conditional compilation (I only get this error when I forget to do them correctly) and make sure that the implementation is not included anywhere.
In response to KodeNerd
KodeNerd wrote:
I am going to think about this for a little bit but I am left wondering why you are implementing your own when there are plenty of pre-made structures

"I'm implementing several data structures to toy around with C++"

I don't plan on using them. I just plan on making them to toy around with the language and try and get back into it. Please read the post. >_>
I'm interested in learning C++, as I am going to learn it in this following year at school.

Sorry for being off topic, but could you tell me the compiling program you use, or some good one(s) ?
In response to Andre-g1
MinGW is great for those that use both Windows and Linux. MVC++ is used for mainly windows development.

I prefer to use MinGW and Code::Blocks for C++ development.

George Gough

[Note]
Popis, a few seconds after you posted I had finished editing my post. Don't forget to read it.
In response to KodeNerd
The whole of my header file:
#ifndef DATA_STRUCTURES
#define DATA_STRUCTURES

//Constants.
const int
SINGLE = 0,
DOUBLE = 1,

LINEAR = 0,
CIRCULAR = 2,

NONE = 0,
FRONT = 1,
BACK = 2;

class node
{
private:
int* value;
int* before, after;

public:
int Read();
bool Write(int);

int* Before();
int* After();

bool SetBefore();
bool SetAfter();
};

class data_structure_archetype
{
private:
int bound;

public:
int Size();
bool IsEmpty();
bool CanAdd(int);
bool SetBound(int);
};

class linked_list
{
private:
int link;
int* first_node, last_node;

void make_links(node, node, node);

public:
int* Get(int, bool);
int* Index(int, bool);
bool Add(int);
bool Insert(int, int);
bool Cut(int, int);
bool Remove(int);
bool Swap(int, int);
bool Empty();
int* Merge(linked_list, bool);
bool Find(int);
int* Copy(int, int, bool, bool);
};

class singly_linked {};
class linearly_linked {};
class doubly_linked {};
class circularly_linked {};
class doubly_circularly_linked {};

class deque
{
private:
int input_restriction;
int output_restriction;

int* front_node, back_node;

public:
int* PushBack(bool);
int* PushFront(bool);
int* PopBack(bool);
int* PopFront(bool);

int* Examine(int, bool);
int* Front(bool);
int* Back(bool);

bool SetInputRestriction(bool, bool);
bool SetOutputRestriction(bool, bool);
};

class stack
{
private:
int* top_node;

public:
int* Push(bool);
int* Pop(bool);

int* Top(bool);
};

class queue
{
private:
int front_node, back_node;

public:
int* Push(bool);
int* Pop(bool);

int* Front(bool);
};

#endif


It's not redefined anywhere, and my conditional compilation stuff is fine.
In response to KodeNerd
Are those free to use, or..
In response to Popisfizzy
Popisfizzy wrote:
It's not redefined anywhere, and my conditional compilation stuff is fine.


But the compiler says it is redefined in structures.cpp?

*shrugs* I don't care for C++ that much cause it can make your head explode.

But does your compiler define any standard libs or something like that which all ready define a node::Read() maybe.

I suppose it might be a strange compiler bug. You could look around on google for similar errors dealing with the compiler your using.

Sorry I couldn't be of much help PIF.
I assume the file I'm staring at (middle and bottom) is structures.cpp? Can I see main.cpp too?
In response to Stephen001
main.cpp:
#include <iostream>

using namespace std;

#include "Structures.cpp"
#include "header.h"

int main()
{
cout << "Hello world!" << endl;
return 0;
}

structures.cpp (currently):
#include "header.h"

//Implementation of the node class.

header.h: http://www.byond.com/members/DreamMakers/forum?id=657602
In response to Popisfizzy
#include "Structures.cpp"


There is your problem right there. You generally speaking only include header files. The linker (ld in your case) is responsible for taking the object files (compiled from the cpp files) and linking them together to get a coherent output object file / EXE.

Compiling generally works like this:

Input files -> Preprocessor -> Compiler -> Linker -> Output

#include is a preprocessor statement, so it just copies the contents of Structures.cpp into main.cpp. The compiler compiles Structures.cpp into Structures.o and main.cpp into main.o. So you've got class implementation twice, once in Structures.o and once in main.o. The linker joins these two object files together, ending up with an output file that has the class implementation twice, hence the error ld gave you.
In response to KodeNerd
KodeNerd wrote:
George Gough

[Edit]
As it states, you must have redefined int node::Read() somewhere else. Double check you conditional compilation (I only get this error when I forget to do them correctly) and make sure that the implementation is not included anywhere.

As Stephen and I said, you included the implementation. I recommend getting a primer or something so that you can get back in the groove of C++ development.

George Gough
         int* before, after;


Someone catch me if I'm wrong since I haven't touched any C/C++ code in awhile but I'm pretty sure that definition will make before an int pointer and after just an int. Anyway should be pretty simple to use the compiler to check just try and dereference after.
In response to Theodis
And it would only make sense that way. There is no such type as int*, but the * in front of the variable name denotes that the variable is a pointer to a variable of type int.

int *before, *after;
In response to Theodis
You are right.

George Gough
In response to CaptFalcon33035
CaptFalcon33035 wrote:
And it would only make sense that way. There is no such type as int*, but the * in front of the variable name denotes that the variable is a pointer to a variable of type int.

In C++ if I remember correctly does treat pointers as intrinsic types.

George Gough
In response to Theodis
Aye, I figured that out when I stumbled upon the error. :P