multiple definition error with Dev-C++

Well I thought it was working...
I seem to be having an error with my code that's assuming I've defined
functions twice. I have a class broken down in header files that seemed to work
but now it does not. here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// FRUITS.h

#include <iostream>
using namespace std;

#ifndef __NEW_FRUITS__
#define __NEW_FRUITS__

class NEWFRUITS
{
      private:
              unsigned short int size;
              char name;
              char color;
      public:
             // creation methods
             void eatFruit();
             void setName(char newName);
             char getName();
             void setColor(char newColor);
             char getColor();
             void setSize(unsigned short int newSize);
             unsigned short int getSize();
             // fruit methods
             void APPLE();
             void ORANGE();
             void BANANA();
};

void NEWFRUITS::setSize(unsigned short int newSize)
{
     size = newSize;
}

unsigned short int NEWFRUITS::getSize()
{
    return size;
}

void NEWFRUITS::setName(char newName)
{
     name = newName;
}

char NEWFRUITS::getName()
{
     return name;
}

void NEWFRUITS::setColor(char newColor)
{
     color = newColor;
}

char NEWFRUITS::getColor()
{
     return color;
}

void NEWFRUITS::eatFruit()
{
     cout << "You take a bit of the " << name << ".\n";
     cout << "It are delicious.\n";
}

#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// newFruitSet.cpp
#include <iostream>
#include "NEW_FRUITS.h"
using namespace std;


void NEWFRUITS::APPLE()
{
     setSize(5);     
}

void NEWFRUITS::ORANGE()
{
     setSize(11);
}

void NEWFRUITS::BANANA()
{
     setSize(87);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

// newFruitsMain.cpp

#include <iostream>
#include "NEW_FRUITS.h"
using namespace std;

int main()
{
    NEWFRUITS banana;
    banana.BANANA();
    cout << banana.getSize() << endl;
    NEWFRUITS apple;
    NEWFRUITS orange;
    
    apple.APPLE();
    orange.ORANGE();
    
    cout << apple.getSize() << endl;
    cout << orange.getSize() << endl;

      
    
    system("PAUSE");
    return 0;
}


with specific methods in the 'newFruitSet.cpp' file
for different sizes of fruit.

as said, it worked great earlier. These are the errors that popped up:
"multiple definition of 'NEWFRUITS::setSize(unsigned short)"
and it did this for all the functions I had declared.

If anyone can get this to work, that would be fantastic. Also if there's
any mistakes or common beginner hangups with the way I coded, please let me know.

Thanks!
-- Mark
Move the function definitions in FRUITS.h to newFruitSet.cpp. What's happening is that by putting the function definitions in the header, and including the header in two sources, you're defining the functions once per source. The linker encounters this and reports it as a duplicate definition error.

By the way, a single char can't hold a string. Look into std::string.
I had tried it this way and it worked.
However I wanted to breakdown specific fruit methods into a separate .cpp file because there would end up being a lot of them.
If i just made the header file and those .cpp files id get a Win32 error or something like that.
It seemed to work if once I made a project file in Dev-C++...well, it worked until it didn't.

Is there a way to split up function definitions into two separate files?
If i just made the header file and those .cpp files id get a Win32 error or something like that.
What do you mean?

Is there a way to split up function definitions into two separate files?
Each function definition is independent of all the rest, so if you really wanted to, you could put each definition in a .cpp of its own.
Other less extreme combinations are also legal, of course.

If i just made the header file and those .cpp files id get a Win32 error or something like that.
What do you mean?

in the past I've been able to create header file that would contain a function prototype
and insert its definition into a seperate cpp file. Which I could then run in another cpp file as long as included the header file at the beginning.
It only works in Dev-C++ if I make a project.
I specifically get this error if I try and split up my header files and function definitions into two files:


[Linker error] undefined reference to 'WinMain@16'
ID returned 1 exit status


The header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// FRUITS.h

#include <iostream>
using namespace std;

#ifndef _FRUITS_
#define _FRUITS_

class FRUITS
{
      private:
              unsigned short int size;
              char name;
              char color;
      public:
             // creation methods
             void eatFruit();
             void setName(char newName);
             char getName();
             void setColor(char newColor);
             char getColor();
             void setSize(unsigned short int newSize);
             unsigned short int getSize();
             // fruit methods
             void APPLE();
             void ORANGE();
             void BANANA();
             
};

#endif


The function definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// fruitSet.cpp

//#include <iostream>
#include "FRUITS.h"
using namespace std;

void FRUITS::setSize(unsigned short int newSize)
{
     size = newSize;
}

unsigned short int FRUITS::getSize()
{
    return size;
}

void FRUITS::setName(char newName)
{
     name = newName;
}

char FRUITS::getName()
{
     return name;
}

void FRUITS::setColor(char newColor)
{
     color = newColor;
}

char FRUITS::getColor()
{
     return color;
}

void FRUITS::eatFruit()
{
     cout << "You take a bit of the " << name << ".\n";
     cout << "It are delicious.\n";
}


void FRUITS::APPLE()
{
     setSize(5);     
}

void FRUITS::ORANGE()
{
     setSize(11);
}

void FRUITS::BANANA()
{
     setSize(87);
}


[Linker error] undefined reference to 'WinMain@16'
Ah, that one. Yes, you do need to create a project.
I just switched to using Visual C++ and suddenly everything works like it should.

Thanks for your help!

-- Mark
Topic archived. No new replies allowed.