operator declaration within namespace in a header? - c++

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;
}

Related

How to create a Class inside of a namespace?

What must the structure of a class look like if it is defined in a separate namespace?
Which parts belong in the header file and which in the cpp file?
How can I make the class accessible only through this specific namespace?
classname.h
#include <iostream>
namespace N {
class classname {
public:
void classmethod();
}
}
classname.cpp
#include "classname.h"
namespace N {
void classname::classmethod() {
std::cout << "classmethod" << std::endl;
}
}
main.cpp
#include "classname.h"
int main() {
N::classname a;
classname b; // Error!
a.classmethod();
return 0;
}

Method Calling from header files

I have a programming assignment where I'm supposed to write up the code for inserting and removing linked lists. However I haven't used C++ in a while and am struggling remember certain things.
Right now, I am simply trying to put a prototype method in a header file, define it in my cpp file, and then call it in my main method. this is what I have.
LinkedList.h
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
LinkedList.cpp
#include "LinkedList.h"
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I am getting the following errors
a nonstatic member reference must be relative to a specific object
'LinkedList::testPrint': non-standard syntax; use & to create a pointer to member
LinkedList::testPrint() is a member function.
It is not declared static, so that means it must be called on a particular object, defined as LinkedList linked_list, for example. Then use linked_list.testPrint().
Option 1 - static member function declaration
#include <iostream>
using namespace std;
class LinkedList {
public:
static void testPrint();
};
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
Option 2 - Instantiated object with call to member function
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
int main() {
LinkedList linked_list;
linked_list.testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}

Why do I not need to include main.cpp?

In the small example needsExtern.cpp needs the definition of global::bar. needsExtern.cpp would normally include the file with the definition (in this case main.cpp). However, since the file is main.cpp it is not needed.
Why does needsExtern.cpp not need to include main.cpp?
needsExtern.h
struct NeedsExtern
{
NeedsExtern();
};
needsExtern.cpp
#include "needsExtern.h"
#include <iostream>
namespace global
{
extern const int bar;
}
NeedsExtern::NeedsExtern()
{
std::cout << global::bar << "\n";
}
main.cpp
#include "needsExtern.h"
namespace global
{
extern const int bar{26};
}
void main()
{
NeedsExtern ne;
}
This is precisely where extern is invented for: the compiler just assumes the variable is defined elsewhere in the project. You can read more about this principle here.

C++ begineer : using namespace error

I am really new in C++, and I can not solve the compilation error below.
data_structure.h
#include <stdint.h>
#include <list>
namespace A {
class B {
public:
bool func_init(); // init
};
};
data_structure.cpp
#include "data_structure.h"
using namespace A;
bool B::func_init(){
std::cout << "test init" << std::endl;
return true;
}
main.cpp
#include <iostream>
#include "data_structure.h"
using namespace A;
int main( int argc, char **argv ) {
A::B s;
s.func_init();
return 0;
}
I have an error as the following
undefined reference to `A::B::func_init()'
Please kindly advice why I can not get the func_init, eventhough it is declared as public? I have put also the correct namespace there.
Would appreciate for any response.
That's a linker error, so you're probably not compiling all your source files, or linking them together, or making some weird use of the C compiler (I see your files have the extension .c, some compilers treat them as C-source).
g++ main.cpp data_structure.cpp -o test should do it.
However I did need to add #include <iostream> to your data_structure.cpp file to resolve
data_structure.cpp: In member function ‘bool A::B::func_init()’:
data_structure.cpp:7:5: error: ‘cout’ is not a member of ‘std’
data_structure.cpp:7:33: error: ‘endl’ is not a member of ‘std’
and make it compile.
The definition of a function has to be in the namespace that declares the function. A using declaration just pulls in names from the namespace; it doesn't put you inside it. So you have to write data_structure.cpp like this:
#include "data_structure.h"
#include <iostream>
namespace A {
bool B::func_init() {
std::cout << "test init" << std::endl;
return true;
}
}
Or, if you prefer, you can use the namespace name explicitly in the function definition:
bool A::B::func_init() {
std::cout << "test init" << std::endl;
return true;
}
Have you tried not putting
using namespace A;
in your data_structure.cpp file and instead putting:
#include "data_structure.h"
bool A::B::func_init() {
std::cout << "test init" << std::endl;
return true;
}
I have the feeling that when you use using namespace A; doesn't let you append function definitions to the namespace, but only tells the compiler/linker where to look for Types or Functions...
Another theory:
Have you tried nesting your CPP code in the same namespace?
#include "data_structure.h"
namespace A {
bool B::func_init() {
std::cout << "test init" << std::endl;
return true;
}
}

Error : **** has not been declared

In my Function.h file:
class Function{
public:
Function();
int help();
};
In my Function.cpp file:
#include "Function.h"
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In my Main.cpp
#include <iostream>
#include "Function.h"
using namespace std;
int menu(){
Function fc;
fc.help();
return 1;
}
int main(int args, char**argv){
return menu();
}
Error is : ‘Function’ has not been declared
Can anybody tell me why? Thank you.
I tried like this and the problem is solved, but I dont really understand why:
In Function.h file:
I use
class Function{
public:
int status;
Function():status(1){}
int help();
};
instead of the old one
class Function{
public:
Function();
int help();
};
All your include statements are missing the #:
#include "Function.h"
^
Everything else looks fine, though you need to also #include <iostream> in Function.cpp since you're using cout.
Here is the Function.cpp that I got to compile and run:
#include "Function.h"
#include <iostream>
int Function::help() // No error here
{
using namespace std;
cout << "Help";
return 1;
}
Function::Function()
{
}
I had a similar problem. Make sure that you only have the required header files. I had two header files both including each other and it spit out this mistake.
You have created a declaration for the constructor of the Function class without including it in your implementation (cpp file).
#include "Function.h"
Function::Function(){
// construction stuff here
}
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In the first Function.h file you have declared the constructor but not defined it. In the second Function.h file (the one that works) you have defined and declared the Function constructor. You can either define and declare in the header or file, or declare in the header file and define in the Function.cpp file.
For example, declare in the header file "Function.h":
class Function
{
Function();
}
and define here in "Function.cpp":
Function::Function(){}
Or the alternative is to declare and define in the header file "Function.h":
Class Function
{
Function(){}
}
The other thing that you have done in the second version of the header file is to initialise the member variable "status" in the "member initialisation list" which is a good thing to do (See Effective C++ by Scott Meyers, Item 4). Hope this helps :)