Multiple definitions in same row

I'm trying to make a physics-ish, thingy, where I imported something from a different program I made, but this time tried putting it in it's own class:
code: http://pastebin.com/EfYbBKmg
With this code I get the error:
[path to program]\Physics3\include\Factor.h|52|multiple definition of `Send(Factor, bool)'|
obj\Debug\src\Factor.o:[path to program]\Physics3\include\Factor.h|52|first defined here|

for every function using Factor.
If I place the factors inside the class, the errors disappears, but then i have to write this: [Factor variable].[command], like nullFactor.Send(nullFactor);

So my questions are:
1: Why does it say there are multiple definitions (rather than some other error)?
2: How can I make it behave the same as if I had the code pasted into the main file?

I'm using code-blocks, and a different class in the same folder works fine
Last edited on
The problem is that the Send functions and the nullFactor object will be redefined for each source file (.cpp) that includes Factor.h. Normally you avoid this by putting only the declarations in the header file and the definitions in a source file.

There are a few exceptions to this multiple definition rule that makes it possible (and it's often necessary) to have class definition, inline function definitions and templates in the header.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Factor.h
#ifndef FACTOR_H
#define FACTOR_H

class Factor
{
	...
};

extern Factor nullFactor;

template<typename T>
void Send(T a)
{
	...
}

void Send(Factor a, bool b);

#endif 


1
2
3
4
5
6
7
8
9
// Factor.cpp
#include "Factor.h"

Factor nullFactor;

void Send(Factor a, bool b)
{
	...
}
Thanks, I put it in Factor.cpp and now it works :3

Only problem is that the program can't find it. If I write:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <Factor.h>

using namespace std;



int main(){
    Send(nullFactor);
    return 0;
}
, then CodeBlocks can see that the functions exist (they appear as suggestions when I type in part of the name), but the compiler tell me they weren't declared in "this scope". Any ideas how to fix this?
It sounds like you may have forgotten to add Factor.cpp to the CodeBlocks project. If that's not the problem, maybe you put the definition of the template function into the .cpp file. You'll notice that Peter87 only moved the definition of the non-template function to the .cpp file. Template functions must be completely specified in the header file.

If neither of those is the problem, paste your code again and we'll see what's up. My ESP is a little bit off today, so I'm having a hard time reading your actual code right now.
You have moved too much. The function templates should be in the header (nothing in the source file). All global variables need to be declared (with extern) in the header.

1
2
3
extern int digits;
extern bool degrees;
...

And all functions need to be declared in the header.

1
2
3
void Send(Factor a, bool b);
void Send(Factor a);
...
If you look at Peter87's post, you will see the following things in the header file:
Line 10: external declaration of the nullFactor global object
Line 18: declaration of "Send" non-template function (2 arguments)
Lines 12 - 16: entire definition of "Send" template function (1 argument)

You moved all of these to the .cpp file. Only the definitions of the non-template functions and the instantiation of the global abject should have been moved to the .cpp file.

These all need to be in the header file. When compiling the file containing main, the compiler has access to the header files that are included, not the source (.cpp) files that are not included. The compiler sees "Send" and "nullFactor" in line 9 and doesn't know what they are. By moving the declarations of these items to the header file, the compiler will know that Send (with one argument) is a template function that can be instantiated as described, and nullFactor is a Factor object that is defined in another module. Without the declarations, main() cannot know about these items, and you get the "weren't declared in this scope" error.
Okay, I tried this: (not completely sure what you meant, but it looked kinda like this)
.h http://pastebin.com/kVK3XJSD
.cpp http://pastebin.com/iJKJe9Aq

And main remains unchanged

Now I get both multiple definitions of digits, and unknown reference to o
Last edited on
Oh, never mind, had to declare them in the cpp file too, and not assign their value in the .h file.
Topic archived. No new replies allowed.