I declare a namespace in file test.h as follows:
namespace dev {
class test {
public:
test();
~test();
int m_number;
};
int sumInt(int, int);
}
When I complete file test.cpp as follows:
using namespace dev;
test::test() {
m_number = 1;
}
test::~test() {}
int sumInt(int a, int b) {
return a + b;
}
There occurs an error "Unresolved external symbol dev::sumInt(int,int)"
So I have to explicit introduce function sumInt to namespace dev like:
int dev::sumInt(int a, int b);
I'm very confused that when I using the namespace dev in file test.cpp,
why should method sumInt() be explicit defined in namespace dev,but class test should't be?
Sorry my English is poor,I have try my best to describe the question clearly.
I hope someone can enlighten me.
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've gone through many similar threads on this kind of question but I'm still unable to resolve this error. Any help would be appreciated.
/*samp.h header file*/
#include<iostream>
using namespace std;
class test
{
private:
test();
public:
static void const_caller();
};
/*samp.cpp file*/
#include<iostream>
using namespace std;
class test
{
private:
test()
{
cout<<"priv cont called\n";
}
public:
static void const_caller()
{
cout<<"calling priv const\n";
}
};
/*main.cpp file */
#include"samp.h"
using namespace std;
int main(int argc, char **argv)
{
test::const_caller();
}
when I do
g++ samp.cpp main.cpp -o main.out
I get this error
/usr/bin/ld: /tmp/ccHZVIBK.o: in function `main':
main.cpp:(.text+0x14): undefined reference to `test::const_caller()'
which I'm unable to resolve since quite some time now.
In the samp.cpp file you define test class again.
You need to include samp.h header and implement methods of test class:
#include "samp.h"
using namespace std;
test::test()
{
cout << "priv cont called\n";
}
void test::const_caller()
{
cout << "calling priv const\n";
}
With your posted code, the .cpp file contains its own definition of the class that has the same name as the class in the .h file but it is really a different class.
In the .cpp file, you need to use:
test::test()
{
cout<<"priv cont called\n";
}
void test::const_caller()
{
cout<<"calling priv const\n";
}
Let's say there is a library named test, the header "test.hpp" is like follows:
namespace test
{
int myfun(int a);
}
And as for the implementaion, which style is better?
#include"test.hpp"
int test::myfun(int a){
return a*a;
}
or
#include"test.hpp"
namespace test
{
int myfun(int a){
return a*a;
}
}
Suppose you have multiple namespaces or nested namespaces in your header as :
namespace test{
namespace subtest{
int Foo(int);
//many other functions go here
} //namespace subtest
} //namespace test
And
namespace test1{
int Foo(int);
}
namespace test2{
int Bar(int);
}
In these cases you should always go with Second implementation as it makes your code more readable and easy to debug.
First one :
#include "test.hpp"
int test::subtest::Foo(int x){return x;}
//many other goes here
Look as the nesting increase everytime to define a function, you need to write fully specified name of the function (repeating namespaces again again).
Second one :
#include "test.h"
namespace test{
namespace subtest{
int Foo(int x){return x;}
//other go here
}
}
This solves namespace name repetition also you can easily refactor things. To debug or refactor a namespace's content simply jump to it's first declaration and change the things. You can also collapse the code under single namespace. (With most ide) making you code more beautiful.
Similarly for multiple namespaces
First one :
#include "test.hpp"
int test1::Foo(int x){return x;}
int test2::Bar(int x){return x;}
How difficult it gets to debug things. Moreover if under two namespace same function name occurs you will have good time debugging.
Second one :
#include "test.hpp"
namespace test1{
int Foo(int x){return x;}
}
namespace test2{
int Bar(int x){return x;}
}
All the declaration within a namespace will be together. So debugging and jumping within namespace will be ease.
Also most open source projects use second implementation
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.
My initial suspicion was that there was a circular dependency in my code and went through Resolve header include circular dependencies. But this hasn't resolved my compilation errors. Here is the code with 3 classes - A, G & N.
//A.h
#ifndef A_H_
#define A_H_
class com::xxxx::test::G;
namespace com { namespace xxxx { namespace test {
class A {
public:
A();
~A();
void method1(void);
private:
G* g;
};
} } }
#endif /* A_H_ */
//A.cpp
#include "A.h"
#include "G.h"
namespace com { namespace xxxx { namespace test {
A::A() {
g = new com::xxxx::test::G();
}
A::~A() {
delete g;
}
void A::method1() {
g->method2(*this);
}
} } }
//G.h
#ifndef G_H_
#define G_H_
class com::xxxx::test::A;
namespace com { namespace xxxx { namespace test {
class G {
public:
void method2(const A&);
};
} } }
#endif /* G_H_ */
//G.cpp
#include "N.h"
namespace com { namespace xxxx { namespace test {
void G::method2(const A& a) {
N n(a, *this);
}
} } }
//N.h
#ifndef N_H_
#define N_H_
#include "A.h"
#include "G.h"
namespace com { namespace xxxx { namespace test {
class N {
public:
N(const A& obj1, const G& obj2) : a(obj1), g(obj2) {}
void method3(void);
private:
A a;
G g;
};
} } }
#endif /* N_H_ */
I am getting about 10 compilation errors in A.h and A.cpp I am listing the compilation errors below:
./src/A.h:11: error: 'com' has not been declared
../src/A.h:25: error: ISO C++ forbids declaration of 'G' with no type
../src/A.h:25: error: invalid use of '::'
../src/A.h:25: error: expected ';' before '*' token
../src/A.cpp: In constructor 'com::xxxx::test::A::A()':
../src/A.cpp:16: error: 'g' was not declared in this scope
../src/A.cpp: In destructor 'com::xxxx::test::A::~A()':
../src/A.cpp:20: error: 'g' was not declared in this scope
../src/A.cpp: In member function 'void com::xxxx::test::A::method1()':
../src/A.cpp:24: error: 'g' was not declared in this scope
What could be the mistake in the above code?
Thank you in advance,
Regards,
Raghava.
The forward declaration
class com::xxxx::test::G;
is illegal. Members of a namespace must be declared within it.
namespace com { namespace xxxx { namespace test {
class G;
Also, as Kenny says, namespaces aren't used like this in C++. Unless your project is distributed as a library or is of reasonably large size (dozens of files minimum), you probably don't need your own namespace.
When the compiler tries to compile a.cop, the first thing it encounters (included from a.h) is this:
class com::xxxx::test::G;
At this point there is nothing to tell the compiler what exactly com, xxxx and test are. Each of these can be either a namespace or a class. This also means that G is unclear, which leads to all other errors.
Is class com::xxxx::test::G; legal in C++ ? I would have written:
namespace com {
namespace xxxx {
namespace test {
class G;
}
}
}
As others have pointed out, using class com::xxxx::test::G; is illegal.
The simpler conversion is (preserving inline-ness):
namespace com { namespace xxxx { namespace test { class G; } } }
I prefer this way of forward declaring because "grepping" does not show scope, whereas this syntax immediately lay it out for all to see.