includes inside a namespace - c++

Is the following approach correct? Well i get a compilation error.
a.hpp is
#include <iostream>
class a
{
public:
void classa_f();
};
a.cpp is
#include "a.hpp"
void a::classa_f()
{
std::cout<< "a::classa_f\n";
}
main.cpp
#include <iostream>
namespace myname {
#include "a.hpp"
}
int main ()
{
myname::a obj;
obj.classa_f();
return 0;
}
I get the following error
g++ main.cpp a.o
/tmp/ccOOf5s7.o: In function main':
main.cpp:(.text+0x11): undefined reference tomyname::a::classa_f()'
collect2: ld returned 1 exit status
Well my question is, is it possible to have just the includes under the namespace but not the actual implementation, because I can see that compiler is searching the namespace for he definition of the function.which is actually not there.

namespace myname {
#include "a.hpp"
}
Declares a class method myname::a::classa_f , which obviously doesn't exist in your program. It's not valid.

In the implementation, you must
namespace myname
{
void a::classa_f()
{
std::cout<< "a::classa_f\n";
}
}
and please remove #include <iostream> from the hpp file, it gets imported into the namespace too.

Related

C++ : undefined reference issue in a simple case of separate compilation

Given the below class definition in the header file - "class1.h"
#ifndef CLASS1_H
#define CLASS1_H
class class1
{
public:
class1 &fcn();
};
#endif
and the member function fcn is defined in the source file - "class1.cpp"
#include "class1.h"
#include<iostream>
inline class1 &class1::fcn()
{
std::cout << "Welcome to Class1" << std::endl;
return *this;
}
when the following code in "main.cpp" is executed
#include <iostream>
#include "class1.h"
int main()
{
class1 myclass;
myclass.fcn();
}
it produces the following error
C:\...\rough>g++ main.cpp class1.cpp && a
C:\...\Local\Temp\ccJvpsRr.o:main.cpp:(.text+0x15): undefined reference to `class1::fcn()'
collect2.exe: error: ld returned 1 exit status
What went wrong?
The inline keyword is the problem. You are supposed to use that with functions that are defined in headers. In your case, remove it and it should work fine.

C++ undefined namespace

I'm trying to create a header file, implementation file and main file for a simple c++ program that returns the systems hostname using gethostname().
my header file that declares my class, data, and methods
//hostname.h
#pragma once
namespace hostnamespace
{
class hostname_class
{
public:
hostname_class();
void to_string();
private:
char *name;
};
}
my implementation file that defines my class and methods
//hostname_class.cpp
#include "hostname.h"
#include <iostream>
#include <unistd.h>
#include <limits.h>
using namespace hostnamespace;
using namespace std;
class hostname_class{
private:
char *name;
public:
hostname_class(){
gethostname(name, HOST_NAME_MAX);
}
void to_string(){
cout << "Hostname:" << &name << endl;
}
};
my main program file
//hostname_main.cpp
#include "hostname.h"
#include <iostream>
using namespace hostnamespace;
int main() {
hostname_class host;
host.to_string();
return 0;
}
when I try and run g++ -o main hostname_main.cpp hostname_class.cpp
I get this error
/bin/ld: /tmp/ccGfbyuu.o: in function `main':
hostname_main.cpp:(.text+0x1f): undefined reference to `hostnamespace::hostname_class::hostname_class()'
/bin/ld: hostname_main.cpp:(.text+0x2b): undefined reference to `hostnamespace::hostname_class::to_string()'
collect2: error: ld returned 1 exit status
any help would be appreciated.
You are redeclaring the hostname_class in the global namespace instead of defining it within the namespace you intended.
A sample structure of how your hostname_class.cpp file could be written.
#include <iostream>
#include <unistd.h>
#include <limits.h>
#include "hostname.h"
using namespace std;
namespace hostnamespace
{
hostname_class::hostname_class() {
gethostname(name, HOST_NAME_MAX);
}
void hostname_class::to_string() {
cout << "Hostname:" << name << endl;
}
};
One side note - I literally copied your method implementations verbatim. It will compile. But name is certainly uninitialized and pointing to an undefined memory address when you pass it into gethostname. That's probably not good.

Including header files in C++ (class definition and method implementation)

I have already checked StackOverflow to find the solution to my problem, but I think I might be missing something. I am trying to define a class in a header file (.h) and implement its methods in a cpp file (.cpp), but it does not work.
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
I don't understand why I keep getting the following error
undefined reference to 'Message::display()'
Compile this with the command g++ -std=c++11 Message.cpp main.cpp

c++ 'undefined reference to' error

I'm having one of those "undefined reference to " errors when compiling a c++ program. I know this is common pitfall, but so far was unable to figure out what I'm doing wrong.
Here's the relevant code. Ex1Two_Sum.h:
#ifndef EX1TWO_SUM_H
#define EX1TWO_SUM_H
#include <vector>
using namespace std;
namespace ddc {
class Ex1Two_Sum
{
public:
void f();
protected:
private:
};
}
#endif
Ex1Two_Sum.cpp:
#include <vector>
#include <cstddef>
#include <iostream>
using namespace std;
namespace ddc {
class Ex1Two_Sum {
public:
void f(){
cout << "works" << endl;
}
};
}
And finally, main.cpp:
#include <iostream>
#include "Ex1Two_Sum.h"
using namespace std;
using namespace ddc;
int main()
{
Ex1Two_Sum ex1;
ex1.f();
return 0;
}
I compile as follows:
g++ -std=c++11 -c Ex1Two_Sum.cpp
g++ -std=c++11 -c main.cpp
g++ Ex1Two_Sum.o main.o
yielding the following message:
main.o: In function `main':
main.cpp:(.text+0x2c): undefined reference to `ddc::Ex1Two_Sum::f()'
collect2: error: ld returned 1 exit status
Your source file redefines the whole class, with an inline function definition, when it just needs to provide a non-inline function definition.
#include "Ex1Two_Sum.h"
void ddc::Ex1Two_Sum::f() {
std::cout << "should work\n";
}
Also, please don't put using namespace std; in a header. Not everyone wants the global namespace polluted in potentially surprising ways.
First, which line of the command throws that error?
Second, I think you forgot to include the Ex1Two_Sum.h in the Ex1Two_Sum.cpp
Third you need to change class ....... in Ex1Two_Sum.cpp to:
void Ex1Two_Sum::f(){...}

Error in including a Global Vector when used in multiple files

// Main.cpp
#include <iostream>
#include "common.h"
#include "second.cpp"
#include <vector>
int main(){
global = 10;
ip.push_back("TestTest");
std::cout << global << std::endl;
TestClass t;
t.print();
}
//common.h
#ifndef GLOBAL_H
#define GLOBAL_H
#include <vector>
#include <string>
extern int global;
extern std::vector<std::string> ip ;
#endif
// second.cpp
#include <iostream>
#include "common.h"
int global;
class TestClass{
public:
void print();};
void TestClass::print(){
global++;
std::cout << "Global: "<<global << std::endl;
std::cout << "IP String: "<<ip[0] << std::endl;
}
// Console Error
ubuntu:deleteme$ g++ main.cpp
/tmp/ccoJpYRl.o: In function `TestClass::print()':
main.cpp:(.text+0x55): undefined reference to `ip'
/tmp/ccoJpYRl.o: In function `main':
main.cpp:(.text+0xdd): undefined reference to `ip'
collect2: error: ld returned 1 exit status
The above works when I am just using with the int global variable. However when I added a vector ip to the common.h I am getting the showed error.
This seems like a elemental thing but couldn't get an answer.
Thanks in advance :)
You didn't define the std::vector<std::string>.
With extern you just declard that it's global but defined in another place.
You should add the definition under your int global in second.cpp
// second.cpp
#include <iostream>
#include "common.h"
int global;
std::vector<std::string> ip;
class TestClass{
As an aside, you shouldn't use globals.