C++ Constructor not showing output - c++

I have the following 3 files, main.cpp and then a class defintion including a header file for the class:
main.cpp:
#include <iostream>
#include "data_vars_class.hpp"
int main () {
DataVars dataVars();
return 0;
}
data_vars_class.hpp:
#include <string>
#include <vector>
class DataVars
{
private:
std::vector<std::string> csv_card_names;
public:
DataVars();
void getCSVData();
};
data_vars_class.cpp:
#include <iostream>
#include <vector>
#include <string>
#include "data_vars_class.hpp"
DataVars::DataVars()
{
std::cout << "constructor?";
getCSVData();
}
void DataVars::getCSVData()
{
std::cout << "Getting csv data!";
}
The problem is when I build and execute the code, I just get an empty terminal. I know that both data_vars_class.hpp and data_vars_class.cpp are being included with the build, this is my build command in Geany:
g++ main.cpp data_vars_class.cpp -o a.out
How come i'm not seeing the cout output in the terminal, like in the constructor shouldnt i see "constructor?" in the terminal?
Thanks

by building the binary file gets created to run the code you have to write ./a.out in the terminal after building it.
g++ main.cpp data_vars_class.cpp -o a.out
./a.out

Related

Undefined reference to class from namespace in C++

I'm new to C++ and trying to do a small quant project with paper trading.
I have a header file alpaca/client.h as follows:
#pragma once
#include <iostream>
#include <../utils/httplib.h>
#include <config.h>
using namespace std;
namespace alpaca {
class Client {
private:
alpaca::Config* config;
public:
Client();
string connect();
};
}
The implementation in alpaca/client.cpp is
#include <iostream>
#include <string>
#include <client.h>
#include <httplib.h>
using namespace std;
namespace alpaca {
Client::Client() {
config = &alpaca::Config();
};
string Client::connect() {
httplib::Client client(config->get_url(MARKET));
auto res = client.Get("/v2/account");
if (res) {
return res->body;
}
else {
return "Error in Client::get_account(): " + to_string(res->status);
}
};
}
And my main.cpp is:
#include <iostream>
#include <string>
#include <client.h>
using namespace std;
int main()
{
alpaca::Client client = alpaca::Client();
client.connect();
return 0;
}
However, I see the following error when I try to compile with g++:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\shubh\AppData\Local\Temp\cc765kwL.o:main.cpp:(.text+0x1ca): undefined reference to 'alpaca::Client::Client()'
Could anyone help with what exactly I'm missing? I'm not too sure.
The g++ command I use is g++ -I./src/alpaca src/main.cpp
It looks like you forgot to compile the client.cpp file. The error message is saying that the linker cannot find a definition for the Client class constructor.
Try compiling both main.cpp and client.cpp with the g++ command, like this:
g++ -I./src/alpaca src/main.cpp src/alpaca/client.cpp

C++ clang compiling & linking on ipad, Linker issue

please may you advise how I may compile & run main.cpp while compiling and linking the my_class.cpp & my_class.h class files,please note that this is running on an iPad using the “Code” app by “thebaselab”, which has offline clang++ 13.0. developer says its possible to work using the below method, however theres no output.
I compile seperately:
clang++ main.cpp -c
clang++ my_class.cpp - c
It seems to produce the main.o and my_class.o files so I may run using:
clang++ main.o my_class.o
This doesnt seem to run as no output, please may you advise if you can see the problem in my code or in compiling.?
I believe there is an issue with linking these files together, as when I have the class defined in main there’s no issues.
My code base:
main.cpp
// Created on iPad.
#include <iostream>
#include <vector>
#include "my_class.h"
using namespace std;
int main() {
cout << "Hey\n";
my_class obj1 = my_class("test");
obj1.display();
cout << "Hello World!";
return 0;
}
my_class.h
#ifndef _my_class_H_
#define _my_class_H_
class my_class
{
private:
std::string name = "";
public:
my_class(std::string name_tmp); // No-args constructor // Copy constructor
~my_class(); // Destructor
void display();
};
#endif
my_class.cpp
#include <iostream>
#include "my_class.h"
// 1-args constructor
my_class::my_class(std::string name_tmp){
name = name_tmp;
}
// Destructor
my_class::~my_class() {
std::cout << "Destructing\n";
}
void my_class::display(){
std::cout << name << "\n";
}
Thanks to #Quimby,
I ran clang++ -o a.out main.o my_class.o then just a.out and it works on my iPad.

Undefined reference in a simple makefile test CPP

I was trying to make a simple makefile to learn how to use headers and make in C++, however it doesn't seems to work, returning "undefined reference" at the end. What is going wrong here? I don't believe there are any mistakes in the .cpp or .hpp files, and I searched a bit about makefile to be sure that it should be working.
My main.cpp:
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
int main(int argc, char *argv[]){
test::print();
return 0;
}
For test.hpp and test.cpp:
#include <iostream>
#include <fstream>
#pragma once
class test{
private:
public:
static void print();
};
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
static void print(){
cout << "aaaaaa\n";
}
Finally, the makefile:
all:
rm *.o
g++ -c -g *.cpp
g++ *.o -o main.exe
rm *.o

c++ Getting error after compile

This is my main.cpp
#include <iostream>
#include <string>
#include "Tokenizer.h"
using namespace std;
int input;
int main(int argc, char const *argv[]){
Tokenizer obj("yeet");
cout << obj.getString();
cin >> input;
return 0;
}
This is my Tokenizer.h
#ifndef TOKENIZER_H
#define TOKENIZER_H
#include <string>
class Tokenizer{
public:
Tokenizer(std::string m);
std::string getString();
protected:
private:
std::string token;
};
#endif // TOKENIZER_H
This is my Tokenizer.cpp
#include "Tokenizer.h"
#include <string>
Tokenizer::Tokenizer(std::string m){
token=m;
//code
}
std::string Tokenizer::getString(){
return token;
}
when i compile with g++ it works fine and when i open a.exe i get this error.
The procedure entry point
_ZNSt7_cxx1112basic_stringlcSt11char_traitslcESalcEEC1EPKcRKS^_ could not be located in the dynamic link libary c:\"My project path"
(All files are in same folder.)
and i compiled with int without strings it worked fine i guess it is a error with #include <string>
In Mingw You have to explicitly specify libgcc libstdc++ the libraries. Use the following command
g++ Tokenizer.cpp main.cpp -o main -static-libgcc -static-libstdc++

Linking shared libraries with executables

I have a small doubt in the compilation of a c++ code along with a shared library.
So I have two files main.cpp and sample.cpp.
main.cpp
#include <iostream>
using namespace std;
#include "sample.h"
myStruct obj;
void populateData() {
obj.s = "hello world";
}
myStruct giveData() {
cout << "Inside main: " << obj.s << endl;
return obj;
}
int main() {
populateData();
}
sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
struct myStruct {
std::string s;
void populateData();
};
myStruct giveData();
#endif
sample.cpp
#include "sample.h"
#include <iostream>
#include <boost/python.hpp>
using namespace std;
void myStruct :: populateData() {
cout << giveData().s;
}
BOOST_PYTHON_MODULE(boosts) {
using namespace boost::python;
class_<myStruct>("struct")
.add_property("s", &myStruct::s)
.def("populateData", &myStruct::populateData)
;
}
I compile the program using
g++ -c -fPIC sample.cpp
g++ -c -fPIC main.cpp
g++ -shared -Wl,-soname,boosts.so -o boosts.so sample.o main.o -lpython2.7 -lboost_python
g++ -o main main.o
./main
Now, when I run the main, it populates the string inside the obj. But when I run a python script, that imports the boosts.so, the obj.s is empty.
I am guessing it is because the library boosts.so is not properly linked with the executable main.
How do I fix this?