The following code can compile:
namespace A{
int i;
}
namespace B{
int i;
}
int main(){ return 0; }
But the following code cannot compile:
#define A
#define B
namespace A{
int i;
}
namespace B{
int i;
}
int main(){ return 0; }
The error info is
error: redefinition of 'int {anonymous}::i'
After I define A and B why do the names of namespaces become anonymous?
The used compiler: gcc-4.9.3.
In
#define A
#define B
namespace A{
int i;
}
namespace B{
int i;
}
You define A and B to be nothing. That means your code becomes
namespace {
int i;
}
namespace {
int i;
}
After the preprocessor runs. Since both namespaces become anonymous namespaces the compiler correctly complains that you are redeclaring i.
Remember that when you define something the preprocessor is going to do through your source code and replace all occurrences of that symbol with whatever you defined it to be. Had you done
#define A LONG_NAME_I_DO_NOT_WANT_TO_TYPE
#define B ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE
namespace A{
int i;
}
namespace B{
int i;
}
Then the preprocessor would change the code to be
namespace LONG_NAME_I_DO_NOT_WANT_TO_TYPE{
int i;
}
namespace ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE{
int i;
}
For more information on how the preprocessor works see: GCC - The C Preprocessor
Related
In my previous question, I asked that how can I extern classes in a class, and we could do:
namespace kc
{
using ::A;
using ::B;
}
instead of:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
And now, I want to do something like this for namespaces, without defining the entire namespace again. I tried to use the same code, but it doesn't work: error: using-declaration may not name namespace <namespace_name>
Edit
Actually, I'm making a kernel, and that's my code (pio.hpp):
#pragma once
namespace pio
{
void outb(unsigned short port, unsigned char value);
void outw(unsigned short port, unsigned short value);
unsigned char inb(unsigned short port);
unsigned short inw(unsigned short port);
}
And (k.hpp):
#pragma once
#include <pio.hpp>
#include <cursor.hpp>
namespace k
{
using ::pio;
using ::cursor;
}
I think what you’re looking for is a namespace alias:
namespace A {
namespace B {
struct C {};
}
}
namespace X {
namespace Y = ::A::B;
}
This would allow you to write e.g.
X::Y::C foo;
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.
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()
{
/* */
}
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.
I have simple class in a header file: a.hh
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
a()
{
i = 0;
}
};
#endif
Then i have a file:b.cc
#include <iostream>
#include "a.hh"
using namespace std;
int main(int argc, char** argv)
{
a obj;
obj.i = 10;
cout << obj.i << endl;
return 0;
}
>
Till this point everything is fine.
I compile the code and it compiles fine.
But as soon as i add a vector in the class:
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
vector < int > x;
a()
{
i = 0;
}
};
#endif
I get a compilation error as below:
> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.
What is the problem with declaring a vector here as a member?
You need to #include <vector> and use the qualified name std::vector<int> x;:
#ifndef a_hh
#define a_hh
#include <vector>
class a{
public:
int i;
std::vector<int> x;
a() // or using initializer list: a() : i(0) {}
{
i=0;
}
};
#endif
Other points:
(as commented by EitanT) I removed the additional qualification a:: on the constructor
have a read of Why is "using namespace std" considered bad practice?
declaring a vector as a class member:
#include <iostream>
#include <vector>
using namespace std;
class class_object
{
public:
class_object() : vector_class_member() {};
void class_object::add_element(int a)
{
vector_class_member.push_back(a);
}
void class_object::get_element()
{
for(int x=0; x<vector_class_member.size(); x++)
{
cout<<vector_class_member[x]<<" \n";
};
cout<<" \n";
}
private:
vector<int> vector_class_member;
vector<int>::iterator Iter;
};
int main()
{
class_object class_object_instance;
class_object_instance.add_element(3);
class_object_instance.add_element(6);
class_object_instance.add_element(9);
class_object_instance.get_element();
return 0;
}
1.You need to #include <vector> and using namespace std, then a.hh just like below:
#ifndef a_hh
#define a_hh
#include <vector>
using namespace std;
class a
{
public:
int i;
vector <int> x;
a()
{
i = 0;
}
};
#endif
2. If you don't want to only use std namespace in all your code, you can specified the namespace before type, just like std::vector<int> x;