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.
Related
I am getting a bunch of errors saying "Undefined reference to ...", and cant understand why. I have read other questions with the error "Undefined reference to ...", but the answers doesent work for me. Here is part of my code:
main:
#include <iostream>
#include <armadillo>
#include <string>
#include <DombsMain.h>
using namespace std;
using namespace arma;
int main(){
cout << "Armadillo version: " << arma_version::as_string() << endl;
dombsmain::initilize("test");
return 0;
}
dombsmain.h:
#ifndef DOMBSMAIN_H_INCLUDED
#define DOMBSMAIN_H_INCLUDED
#include <string>
#include <vector>
#include "Body.h"
#include "Constraint.h"
#include <Solver.h>
namespace dombsmain {
extern void initilize(std::string fileName);
extern std::string inputfileName;
...blabla
}
#endif // DOMBSMAIN_H_INCLUDED
dombsmain.cpp:
#include <DombsMain.h>
#include <BallJoint.h>
#include <dombs.h>
#indlude blabla
using namespace std;
using namespace arma;
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
..blabla
I think the error has something to do with namespace dombsmain. DombsMain.h is included both in main and in dombsmain.cpp, but it still says that the variables and functions in the namespace in undefined. I think it might be some conflict with including DombsMain.h int both main and dombsmain.cpp. I tried deleting the #include <DombsMain.h> in main.cpp, but then I couldent call dombsmain::initilize("test");
You just forgot to define std::string inputfileName. For example you can do it in dobsmain.cpp:
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
}
std::string inputfileName = "";
}
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(){...}
// 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.
I am having a Compile issue.
I have one Class
I have one header file
And of course Main to test my work.
But I am getting compile error, it is out of my understanding what I am doing wrong.
Header File:
#ifndef AGENT_H
#define AGENT_H
using namespace std;
class Agent
{
public:
Agent(string);
virtual ~Agent();
private:
string name;
};
#endif /* AGENT_H */
Agent Class (Agent.cpp)
#include "Agent.h"
using namespace std;
Agent::Agent(string _name)
{
this->name = _name;
}
Agent::~Agent()
{
delete this->name;
}
And my Main:
#include <cstdlib>
#include <iostream>
#include "Agent.h"
using namespace std;
int main(int argc, char** argv)
{
Agent agent1("Danila");
return 0;
}
So I am getting such strange error:
undefined reference to `Agent::Agent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/main.cpp:17: undefined reference to `Agent::~Agent()'
/main.cpp:17: undefined reference to `Agent::~Agent()'
Could you guys help me understand whats wrong there?
You need an #include <string> in your header file.
Also, for good practice, keep the using namespaces in your .cpp files, if any.
You compiled without telling the compiler about Agent.cpp. I.e. you need something like this, for g++:
$ g++ main.cpp Agent.cpp -o myprogram
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.