You are not logged in.

#1 2009-11-07 17:12:22

Varreon
Member
Registered: 2008-07-03
Posts: 95

CPP Class issue[Fixed]

I'm trying to write a linked list class in C++. The class compiles fine, but when I include it in my main file, I get errors about multiple function definitions.

What's wrong with my code? Here's a snippet

Chain.cpp

#define NULL 0
typedef struct element{
    char value;
    element* next;
};

class Chain{
public:
    Chain(){
        start->next = NULL;
        size = 0;
    }
    void addItem(char c);
    void deleteItem(int i);
    char getItem(int i);
    void setItem(int i);
    int getSize();
private:
    element* start;
    int size;
};

void Chain::addItem(char c){
    element *e = start;
    element *n;
    for(int x=0;x<size;x++) {
        e = e->next;
    }
    e->next = n;
    n->value = c;
    size++;
}

char Chain::getItem(int i){
    element *e = start;
    if(i>=size) {
        return 0;
    }
    for(int x=0;x<i;x++) {
        e = e->next;
    }
    return e->value;
}

int Chain::getSize(){
    return size; 
}

*edit: included entire file

Last edited by Varreon (2009-11-08 05:19:34)

Offline

#2 2009-11-07 17:19:52

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: CPP Class issue[Fixed]

That snippet doesn't explain the error message - it compiles and runs fine if included in main.


Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

#3 2009-11-07 17:23:10

Varreon
Member
Registered: 2008-07-03
Posts: 95

Re: CPP Class issue[Fixed]

Alright, I posted my entire class.

Offline

#4 2009-11-07 18:07:08

pyokagan
Member
From: Singapore
Registered: 2009-09-01
Posts: 9

Re: CPP Class issue[Fixed]

I might be wrong, but I thought that variables which are scoped inside a function will be destroyed once the function ends?

In this:

void Chain::addItem(char c){
    element *e = start;
    element *n;
    for(int x=0;x<size;x++) {
        e = e->next;
    }
    e->next = n;
    n->value = c;
    size++;
}

A local variable pointer scoped inside a function (element *n) would have undefined results once the function exits.

And I have not heard of creating an instance of a struct using a pointer. My C is very rusty, so can someone enlighten me?

I didn't look at the code thoroughly, but when I pasted the code into a source file with a main() function in it, and used addItem, then getItem, I get junk data, not the char I used in addItem.

Offline

#5 2009-11-07 18:17:19

Varreon
Member
Registered: 2008-07-03
Posts: 95

Re: CPP Class issue[Fixed]

pyokagan, that may very well be the case. Once I get my code running, I'll have it allocate a block of memory using malloc rather than using a local variable.

Do you(or anyone else) know why my code wont compile?

Offline

#6 2009-11-07 18:41:53

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: CPP Class issue[Fixed]

Where does start point?  I see it's declared as a pointer, but no memory is reserved for its referent in your constructor, or anywhere else for that matter.  It looks to me like you're dereferencing a garbage pointer.

Forgive me if I'm way off the mark; I know very little C++.

Offline

#7 2009-11-07 18:47:24

pyokagan
Member
From: Singapore
Registered: 2009-09-01
Posts: 9

Re: CPP Class issue[Fixed]

Not talking to account the implementation itself, since the code is syntactically correct:

In order to diagnose the problem, the whole compile log would be nice to pinpoint the problem.

Information on how you go about compiling the code would be nice too.

The code will compile fine if you just paste the contents of chain.cpp into a file with a main() function, so I guess it will not compile because of the way you organised the files and how you compiled the code.

The most common problem why multiple definition errors exist is because the file containing the declaration/definition (whatever you call it) of the class are somehow included multiple times.

This is usally solved by putting a macro in the file which makes it so the class is only declared/defined once.

Offline

#8 2009-11-07 19:24:09

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: CPP Class issue[Fixed]

Works for me as well. As is the code compiles and runs just fine.


Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

#9 2009-11-07 20:20:14

Varreon
Member
Registered: 2008-07-03
Posts: 95

Re: CPP Class issue[Fixed]

Well, the way I'm currently compiling is the build command from within Code::Blocks(using mingw).

Compile errors:

obj\Debug\Chain.o||In function `ZN5Chain7addItemEc':|
C:\dev\mchain\Chain.cpp|23|multiple definition of `Chain::addItem(char)'|
obj\Debug\main.o:C:\dev\mchain\Chain.cpp|23|first defined here|
obj\Debug\Chain.o||In function `ZN5Chain7getItemEi':|
C:\dev\mchain\Chain.cpp|34|multiple definition of `Chain::getItem(int)'|
obj\Debug\main.o:C:\dev\mchain\Chain.cpp|34|first defined here|
obj\Debug\Chain.o||In function `ZN5Chain7getSizeEv':|
C:\dev\mchain\Chain.cpp|45|multiple definition of `Chain::getSize()'|
obj\Debug\main.o:C:\dev\mchain\Chain.cpp|45|first defined here|
||=== Build finished: 6 errors, 0 warnings ===|

Start is meant to point to the first element in the list. I intended it to point to the element created through each call off addItem(), but I'll need to fix that.

Offline

#10 2009-11-07 20:24:06

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: CPP Class issue[Fixed]

Are you by any chance including Chain.cpp in main.cpp?


Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

#11 2009-11-07 20:30:57

Varreon
Member
Registered: 2008-07-03
Posts: 95

Re: CPP Class issue[Fixed]

Yes, I am.

main.cpp

#include <stdio.h>
#include "Chain.cpp"

int main(){
    Chain *c = new Chain();
    printf("Size: %d",c->getSize());
    c->addItem('a');
    c->addItem('b');
    c->addItem('c');
    printf("%c",c->getItem(0));
    printf("%c",c->getItem(1));
    printf("%c",c->getItem(2));

}

Offline

#12 2009-11-07 20:46:18

grey
Member
From: Europe
Registered: 2007-08-23
Posts: 679

Re: CPP Class issue[Fixed]

Don't.

See also: source files vs. header files, and definition vs. declaration.


Good ideas do not need lots of lies told about them in order to gain public acceptance.

Offline

Board footer

Powered by FluxBB