Question

I've posted this question because I'm stuck in a very strange problem. I have a project of few .cpp files and few headers. In particular this piece of code appears in three different .cpp files:

void printGraph2d(std::vector < std::vector <plotData> > data, double dy, double dt, const unsigned int M, unsigned int intervalliTemporali);
void printGraph3d(std::vector < std::vector <plotData> > data, double dy, double dt, const unsigned int M, unsigned int intervalliTemporali);
double alpha = 0.000217f;
double hBarra = 0.04f;
double uWall = 100;
double dt = 0.0001f;
const unsigned int Y = 41;
const unsigned int M = 15000;
double dy = hBarra/(Y - 1);
double grid [M][Y];
std::vector <std::array <double, 4> > tridiagonal;
unsigned int intervalliTemporali;

and have a different role in each file. I wonder why the compiler gets me "multiple definitions error". The two function prototypes and the variables are defined ONLY in the .cpp files (not in any headers) and are used only in the files where they are declared.

I solved this error putting the word "static" before the declaration, it worked fine, but I had some troubles with the debugger (not showing the values of the variables). I found out that if I renamed the variables and the functions above, the troubles with the debugger would disappear. Instead of rename the variables, I decided to declare the variables inside a function and not globally. I have not renamed the functions. Doing this way the program crashes every time I call the function "cranknicolson1()" suddently when it is called and before doing anything. I think that this very strange behaviour is caused by the word "static" before the function prototypes, because I can't explain it!!! Here is my code. http://qpwoei.altervista.org/Code.zip I underline the fact that the code crashes in cranknicolson1() before doing anything! So there is no problem with cranknicolson algorithm, but something else. Hope someone can help me, thank you very much in advance!!

Was it helpful?

Solution

You are getting "multiple definitions error" because you have multiple definitions of the same externally visible name. That's illegal. It violates the "one definition rule".

You can put those in an unnamed namespace, as mike.did suggests or qualify them as static, which is what you did. However, both solutions skirt the real issue: Why do you have so many global variables? Your code is not very object oriented.

Your problem with your function cranknicolson1 is distinct from this issue. Ask that as a separate question.

OTHER TIPS

You need to reduce those definitions scope to the corresponding .cpp files by using anonymous namespaces. Otherwise, the symbols are published and could be used in other translation units.

You could also read about Why are unnamed namespaces used and what are their benefits?, What does putting a structure in an anonymous namespace do?, Unnamed/anonymous namespaces vs. static functions and other supporting topics here (just follow the links).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top