Error already defined - c++

Hi i just created a sample class and using it in main but i am getting already defined error.
sample.h
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
#endif
sample.cpp
#include "sample.h"
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
main.cpp
#include <iostream>
#include "sample.h"
using namespace std;
int main(void)
{
int test = count;
return 0;
}
Link error:
main.obj : error LNK2005: "int count" (?count##3HA) already defined in sample.obj
if u see above class i am using #ifndef and #define, actually there things will declare data once thought we include in many places.could some one explain me clearly why its giving that link error.

Remember that #include literally means "add the contents of this file here".
Include guards only protects against a file's content being included more than once per file it's included in.
When the preprocessor has done its preprocessing, this is what your compiler sees:
sample.cpp
[iostream contents here...]
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
main.cpp
[iostream contents here...]
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
using namespace std;
int main(void)
{
int test = count;
return 0;
}
As you can see, there are two definitions of count, one in each file (formally, "translation unit").
The solution is to have a declaration of the variable in "sample.h"
extern int count;
and have the one and only definition in sample.cpp:
int count = 10;
(And you should not put using namespace std; in a header.)

To make a global variable like that visible everywhere:
blah.h
extern int count;
blah.cpp
int count(10);

Include guards only guard against including the same header file multiple times, not against multiple definitions. You should move your variable in a cpp file in order to not violate the ODR, or use internal linkage or declare it external and define it somewhere once. There are multiple solutions depending on the use of that variable.
Notice that I'm ignoring the fact that you probably meant int sample::Get() in the sample.cpp file
#include "sample.h"
sample::sample()
{
cout<<"hello two";
}
int sample::sample() // ??
{
return 10;
}

You have either to declare variable count as having internal linkage as for example
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
namespace
{
int count = 10;
}
//...
#endif
(the above internal declaration valid in C++ 2011) or
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
static int count = 10;
//...
#endif
Or to declare it as having external linkage but define it only once in some module. Fpr example
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
extern int count;
//...
#endif
#include "sample.h"
int count = 10;
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
Otherwise the compiler will issue an error that variable count is defined more than once that is that more than one compilation unit (in this case sample.cpp and main.cpp) contain the variable definition.

Related

namespace extern variable already defined

I cannot compile my C++ program and I don't understand why.
Here's a simple representation of what is throwing errors:
hello/hello.cpp
#include "hello.h"
namespace MyHelloNS {
MyHelloClass::MyHelloClass() {
MyHelloVAR1 = "hi";
MyHelloVAR2 = "dog";
}
}
hello/hello.h
#pragma once
#include <string>
using namespace std;
namespace MyHelloNS {
extern string MyHelloVAR1;
extern string MyHelloVAR2;
class MyHelloClass;
}
class MyHelloNS::MyHelloClass {
public:
MyHelloClass();
};
main.cpp
#include "hello/hello.h"
int main() {
MyHelloNS::MyHelloClass hi1;
}
I get two kinds of errors:
unresolved external symbol in hello.obj
What's wrong?
Add this to main.cpp (or hello.cpp)
namespace MyHelloNS {
string MyHelloVAR1;
string MyHelloVAR2;
}
This question has nothing to do with namespaces, you just aren't following the correct procedure to define a global variable.

multiple definition, already defined here

I have three simple files.
”banana.cc“
namespace ocr{
int a = 5;
}
"apple.cc"
#include "banana.cc"
namespace ocr{
int b = a;
}
"main.cc"
#include "apple.cc"
int main()
{
return 0;
}
/tmp/ccs6XmP2.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
/tmp/ccs6XmP2.o:(.bss+0x0): multiple definition of `ocr::b'
/tmp/ccEkxDgJ.o:(.bss+0x0): first defined here
/tmp/cco0dUCm.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
after compiler insert all the #include, main.cc is like:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
why this will cause redefinition?
Thank you.
Because you're compiling apple.cc and banana.cc and main.cc in your project.
So you're compiling this file:
namespace ocr{
int a = 5;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
Obviously ocr::a is defined in all three files, and ocr::b is defined in two of them.
Don't have enough reputation to comment but I just want to elaborate on this, just in case there is still confusion.
If you want to share some variables between files, create a header file and declare it there.
i.e.
// common.h
namespace ocr{ int a, b; }
// banana.cc
#include "common.h"
void initAppple(){
ocr::a = 4;
}
// apple.cc
#include "common.h
void initBanana(){
ocr::b = a;
}
// main.cc
#include "common.h"
int main(){ initApple(); initBanana(); }
And then when you compile main.cc, link it with apple.cc and banana.cc instead of "including" it.
g++ main.cc apple.cc banana.cc -o output
Note that you can't just declare and initialize separately in global scope, which is why you probably need to use a setter function such as the ones above (initApple() etc). Or use extern inside the header file and define it inside the source file.

How to pass multi-dimensional array to header file in C++

I am trying to work with multi-dimensional arrays.
My goal is to have a separate file for my matrix functions, however I am having trouble with setting the value of V.
Error : ‘V’ was not declared in this scope
Since this error statement is very broad, I could not find a satisfactory answer on my searches.
This is what I want to implement.
main.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int main()
{ int V = 5;
int graph[V][V] = { {... },
{... },
{... },
{... },
{... } };
func1(graph);
func2(graph);
return 0;
}
prims.h
#ifndef PRIMS_H
#define PRIMS_H
#include <bits/stdc++.h>
using namespace std;
int func1(int graph[V][V]);
int func2(int graph[V][V]);
#endif
prims.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int func1(int graph[V][V])
{
// function
}
int func2(int graph[V][V])
{
// function
}
Please comment below if more clarification is required.
Thank you.
Since you want to set the value from main, one alternative is to declare V as global variable in main and as extern const int in prims.h, so that it is visible in prmis.cpp as well.
prims.h
extern const int V;
main.cpp
const int V = 5; //declared as global in main
int main()
{
/* */
}

C++ error Undefined reference to Class::Function()

I am farily new to C++ and I have been stuck with this problem for a few hours now. I am trying to setup the foundations for a video game related experience calculator, but I can't get past this problem.
main.cpp
#include <iostream>
#include "Log.h"
using namespace std;
int main()
{
Log Logs;
enter code here
struct ChoppableLog Yew;
Logs.initialiseLog(Yew, 60, 175);
return 0;
}
Log.h
#ifndef LOG_H
#define LOG_H
struct ChoppableLog
{
int level;
int xp;
};
class Log
{
public:
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int);
Log();
};
#endif // LOG_H
Log.cpp
#include "Log.h"
#include <iostream>
using namespace std;
Log::Log()
{
}
void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}
The error I get is
C:\Users\Murmanox\Documents\C++\C++ Projects\CodeBlocks\Class Files Test\main.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|
I can post more details if necessary.
You have to define Log::initialiseLog with its full name, like so:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{ }
What you are doing is defining a new, free function of the name initialiseLog instead of defining the member function of Log.
This leaves the member function undefined, and, when calling it, your compiler (well, technically linker) will be unable to find it.
The definitions of functions in a header file should specify the scope. In your case, you should define initialiseLog() function in your cpp file as follows:
void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{
}

namespaces in c++

How to use namespaces in C++ where it is accessible in different header files. Lets say I have this below:
// namespaces
#include <iostream>
using namespace std;
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
and I want t use var variable from first namespace in another class... that is defined and implemented in another .h and .cpp file?
//server.h
#ifndef SERVER_H
#define SERVER_H
class server{
server();
//blah
};
#endif SERVER_H
//server.cpp
server::server()
{
first::var = 3;
}
is this possible to do it like this? When I try I get an error that says that my namespace is not defined. And if i put using namespace first in the .h or .cpp it says there is no namespace called first...
Besides having the namespace in a header, you need to make the variable extern:
//header.h
namespace first
{
extern int var;
}
//implementation.cpp
#include "header.h"
namespace first
{
int var = 5;
}
If the variable is not extern, a symbol will be generated wherever the header is included, and you'll get linker errors.
If you don't want the extra header, you can just declare the variable as extern in the same namespace where you want to use it:
//server.cpp
namespace first
{
extern int var;
}
server::server()
{
first::var = 3;
}
Note some answers might claim that you should make the variable static. This is wrong, although it will compile, as then the variable won't act as a global. A copy of it will be created for every translation unit.