This works fine when the function someF is placed in main, it also works fine in its own file if I passed myrect by pointer to it instead of by reference, but it does not work when it's a pass by reference - or by value - in its own file, it gives an error:
"main.obj : error LNK2005: "void __cdecl someF(class Rectangle &)" (?someF@@YAXAAVRectangle@@@Z) already defined in Function.obj"
I don't think that that is the way the pragma once should be used. That is your main error.
One should not include .cpp-files. The convention is to compile each .cpp separately and they merely include necessary declarations and definitions that are in header files.
Yeah, I know. I tried #ifndef with the same result, I just added #pragma once in everything because I started to get desperate and started trying everything.
My original problem was a 5000+ project where this problem happens between 2 huge classes, this is just a stripped down version just to have a minimal code for the question.
Your IDE is probably set up so that it compiles each .cpp file and then links them all together. This would lead to someF being defined multiple times, once for Function.cpp, and once for main.cpp because it includes Function.cpp.
You should never have to include .cpp files. Instead create a header file that declares the function and include that instead.
Function.h
1 2 3 4
#pragma once
#include "Rectangle.h"
void someF(Rectangle &myrect);
Ok, the issue turned out to be VS is as weird and confusing as always, it's a debug build only error that does not happen in release build, and I really doubt it will fail in other compilers. God I hate this stupid IDE and compiler sometimes.
Adding "/FORCE:MULTIPLE" to the debug linker command line options solves it for debug mode.
If you were doing things correctly you wouldn't need to use FORCE:MULTIPLE. You're probably still including a .cpp file somewhere or are defining (non-inline) functions in .h files.
All I can think of is if there's something strange in stdafx.h. Otherwise I don't see anything wrong. The only thing I can suggest is to start a whole new project and see if that fixes it, but you probably already did that.
Are you saying that if you change Function.hpp and Function.cpp so that someF takes the Rectangle by value (which won't change it in main, of course) then it works without FORCE:MULTIPLE?
BTW, "pragma once" doesn't do anything in a cpp file, so you should take it out of main.cpp.
Ok, scratch the /FORCE:MULTIPLE part, it does nothing, VS is even dumber than I thought. To make it work, build the solution, it will fail and give the error, now do any dummy change to the Function.cpp file, like adding a useless line or space, and now it will build and run fine.
By the way, the exact files build fine in other IDEs without the nonsense, and I always use clean when I get weird errors, it's does not solve the problem.
VS is one of the best IDEs. You probably had pre-built stuff that the project thought didn't need to be rebuilt, so regular builds would continue to have linker errors. Perhaps you didn't "Rebuild" one such dependency (adding characters made it think otherwise).
I'm no fan, but it's very dumb for you to think this. You haven't found the problem yet.
99.999% of the time when a beginner complains about "weird" something-or-other, they are blaming the system for their own mistake.
VS is dumb, I had too many dumb things happen that does not happen on any other IDE, I only use it because of its great debugger. Tell me when you find the problem cause I'm pretty sure it's an internal VS linker bug, it works fine in other IDEs and it works in VS when I add a useless space, that is pretty Goddamn dumb.