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.
Related
So I am currently making a small prototype for a bigger project I'm working on and I've gotten completely stuck on it. I am rather new to c++ and haven't worked with headers or namespaces before ever. The issue is that when i try to use my created namespace it fails completely and the compiler (clang) returns undefined.
#include <iostream>
#include "bark.hpp"
using namespace bark;
int main() {
bark::woof();
}
Header file:
#pragma once
#ifndef FUNCTIONS_HPP
#define FUNCTIONS_HPP
namespace bark {
void woof();
}
#endif
file with functions:
#include <iostream>
#include "bark.hpp"
void woof() {
std::cout << std::endl << "woof" << std::endl;
}
In the implementation file, this definition:
void woof()
{
// ...
}
defines a function woof in the global namespace. So when you make the call:
bark::woof();
in the main function, there is no bark::woof defined, and the linker will refuse to link the program.
To correctly define the woof from namespace bark, you need to either qualify it:
void bark::woof()
{
// ...
}
or else introduce the namespace bark, and define it inside:
namespace bark
{
void woof()
{
// ...
}
}
Can you explain me why my function AllToAll is undefined in my example? I use CMake to generate a libNeuralNetwork.a which is called by the exemple.
LayerFactory.hpp
#pragma once
#include "LayerModel.hpp"
#include "Layer.hpp"
namespace nn
{
extern internal::LayerModel AllToAll(int numberOfNeurons, activationFunction activation = sigmoid);
}
LayerFactory.cpp
#include "LayerFactory.hpp"
#include "AllToAll.hpp"
using namespace nn;
using namespace internal;
LayerModel AllToAll(int numberOfNeurons, activationFunction activation)
{
LayerModel model
{
allToAll,
activation,
numberOfNeurons
};
return model;
}
NeuralNetwork.hpp
#pragma once
#include "layer/LayerModel.hpp"
#include "layer/LayerFactory.hpp"
namespace nn
{
class NeuralNetwork
{
public:
NeuralNetwork(int numberOfInputs, std::vector<internal::LayerModel> models);
//...
};
}
Example.cpp
#include "../src/neural_network/NeuralNetwork.hpp"
using namespace nn;
int example1()
{
NeuralNetwork neuralNetwork(3, {AllToAll(5), AllToAll(2)});
}
error message:
CMakeFiles/UnitTests.out.dir/ExamplesTest.cpp.o: In function `example1()':
ExamplesTest.cpp:(.text+0x8b3): undefined reference to `nn::AllToAll(int, nn::activationFunction)'
You have declared AllToAll in the top-level namespace and defined it in the nn namespace.
The following will not declare the function in the namespace:
using namespace foo;
extern void Bar();
You need:
namespace foo {
extern void Bar();
}
I have the following set of files.
// HeaderOne.h
namespace foo
{
namespace bar
{
class A
{
.
.
.
};
}
}
// HeaderTwo.h
namespace foo
{
namespace bar
{
std::string const baz;
}
}
// HeaderTwo.cpp
#include "HeaderTwo.h"
using namespace foo::bar;
std::string const baz = "baz";
// HeaderOne.cpp
#include <iostream>
#include "HeaderOne.h"
#include "HeaderTwo.h"
using namespace foo::bar;
A::A()
{
std::cout << baz << std::endl;
}
I am using CMake as my build system and when I build, I get an undeclared identifier error on baz. I would think this should work, but any changes I make seem to still produce errors between definitions split among the files. Any ideas what might be going wrong here?
When you define something, use this form.
namespace name{
type definedname= value;
}
Or
type name::definedname= value;
Otherwise how could compiler know which namespace the definedname should be put under. It will put under no namespace.
I am trying to use forward declarations in header files to reduce the number of #include used and hence reduce dependencies when users include my header file.
However, I am unable to forward declare where namespaces are used. See example below.
File a.hpp:
#ifndef __A_HPP__
#define __A_HPP__
namespace ns1 {
class a {
public:
a(const char* const msg);
void talk() const;
private:
const char* const msg_;
};
}
#endif //__A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
using namespace ns1;
a::a(const char* const msg) : msg_(msg) {}
void a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef __CONSUMER_HPP__
#define __CONSUMER_HPP__
// How can I forward declare a class which uses a namespace
//doing this below results in error C2653: 'ns1' : is not a class or namespace name
// Works with no namespace or if I use using namespace ns1 in header file
// but I am trying to reduce any dependencies in this header file
class ns1::a;
class consumer
{
public:
consumer(const char* const text) : a_(text) {}
void chat() const;
private:
a& a_;
};
#endif // __CONSUMER_HPP__
Implementation file consumer.cpp:
#include "consumer.hpp"
#include "a.hpp"
consumer::consumer(const char* const text) : a_(text) {}
void consumer::chat() const {
a_.talk();
}
Test file main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
UPDATE:
Here is my very contrived working code using the answer below.
File a.hpp:
#ifndef A_HPP__
#define A_HPP__
#include <string>
namespace ns1 {
class a {
public:
void set_message(const std::string& msg);
void talk() const;
private:
std::string msg_;
};
} //namespace
#endif //A_HPP__
File a.cpp:
#include <iostream>
#include "a.hpp"
void ns1::a::set_message(const std::string& msg) {
msg_ = msg;
}
void ns1::a::talk() const {
std::cout << msg_ << std::endl;
}
File consumer.hpp:
#ifndef CONSUMER_HPP__
#define CONSUMER_HPP__
namespace ns1
{
class a;
}
class consumer
{
public:
consumer(const char* text);
~consumer();
void chat() const;
private:
ns1::a* a_;
};
#endif // CONSUMER_HPP__
File consumer.cpp:
#include "a.hpp"
#include "consumer.hpp"
consumer::consumer(const char* text) {
a_ = new ns1::a;
a_->set_message(text);
}
consumer::~consumer() {
delete a_;
}
void consumer::chat() const {
a_->talk();
}
File main.cpp:
#include "consumer.hpp"
int main() {
consumer c("My message");
c.chat();
return 0;
}
To forward declare class type a in a namespace ns1:
namespace ns1
{
class a;
}
To forward declare a type in multiple level of namespaces:
namespace ns1
{
namespace ns2
{
//....
namespace nsN
{
class a;
}
//....
}
}
Your are using a a member of consumer which means it needs concrete type, your forward declaration won't work for this case.
For nested namespaces, since C++17, you can do
namespace ns1::ns2::nsN
{
class a;
}
Apart to forward-declare the class from within its namespace (as #billz says), remember to either use (prepend) that namespace when referring to the forward-declared class, or add a using clause:
// B.h
namespace Y { class A; } // full declaration of
// class A elsewhere
namespace X {
using Y::A; // <------------- [!]
class B {
A* a; // Y::A
};
}
Ref: Namespaces and Forward Class Declarations
Please excuse me but I didn't know to give a name to the title in a short way.
Why do I need to declare an overloaded operator inside the header to make it work in this example:
HEAD.H
#pragma once
namespace test {
class A {
public:
A() : x(0) {}
int x;
};
A& operator++(A& obj); //this is my question
}
HEAD.CPP
#include "head.h"
namespace test {
A& operator++(A& obj) {
++obj.x;
return obj;
}
}
MAIN.CPP
#include <iostream>
#include "head.h"
using namespace std;
using namespace test;
int main() {
A object;
++object; //this won't work if we delete declaration in a header
return 0;
}
operator++ is defined and declared in a namespace inside "head.cpp" so why do I need to declare it one more time in a header?
Thank you.
The CPP files are compiled independently of each other, and they only see the header files they've included (which are in fact textually added to the source code of the CPP before compilation). As such you'll use the header file to inform the compiler that a function exists with that signature (be it an operator overload).
Then the output from your CPP files is put together by the linker, which is when you'd find out if for instance you had declared a function in a header file but never taken the trouble to implement it.
Simple example with namespaces:
#include <iostream>
namespace test{
int f() { return 42; }
int g() { return -1; }
}
namespace other{
int f() { return 1024; }
}
using namespace other;
int main(){
//error: 'g' was not declared in this scope
//std::cout << g() << std::endl;
std::cout << test::f() << std::endl; //42
std::cout << f() << std::endl; //1024
return 0;
}