c++ Getting error after compile - c++

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++

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

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

C++ does not name a type in Constructor definition

I try to compile my code, I pretty sure I made a mistake in my headers or in the compilation but I don't understand where. I know that this is a basic problem, and I read some other topic, but I really don't understand. And I watch some other code I wrote and I don't see any difference...
g++ -c main.cpp -o out
I don't understand the error, so I also try :
g++ -c readFastqFile.cpp
The error
readFastqFile.cpp:8:1: error: ‘readFastq’ does not name a type
readFastq::readFastq(){ //Constructor
And my files are :
main.cpp
#include <iostream>
#include <string>
#include "readFastqFile.hpp"
using namespace std;
int main(int argc, char *argv[]){
cout << "hello" <<endl;
//readFastq allReads;
return 0;
}
readFastqFile.hpp
#ifdef READFASTQFILE_HPP
#define READFASTQFILE_HPP
#include <iostream>
#include <string>
using namespace std;
class readFastq{
public:
readFastq(); //constructor
private:
string readName;
string sequence;
string score;
};
#endif // READFASTQFILE_HPP
readFastqFile.cpp
#include <string>
#include <iostream>
#include "readFastqFile.hpp"
using namespace std;
readFastq::readFastq(){ //Constructor
readName = "bla";
cout << readName <<endl;
}
Thanks
#ifdef READFASTQFILE_HPP should be #ifndef READFASTQFILE_HPP. The #ifdef is causing the contents of readFastqFile.hpp to be ignored, so the class definition isn't being compiled.
See also Include guards

odb/pgsql/version.hxx no such file or directory

I'm trying to learn how to use C++ and ODB following this tutorial:
http://www.codesynthesis.com/products/odb/doc/manual.xhtml#2
I've created a Person.hxx file where there is the declaration of class Person as persistent, then I've got thre files Person-odb: .cxx, .hxx, .ixx
Now I should compile Person-odb.cxx with
g++ -I/usr/lib/odb/i686-linux-gnu/include Person-odb.cxx
but it end with:
fatal error: odb/pgsql/version.hxx: No such file or directory. compilation terminated.
I see that there is a file version.hxx but there's no odb/pgsql directory...
what's wrong?
this is Person.hxx where I have defined the persistent class Person:
#ifndef PERSON_HXX
#define PERSON_HXX
#include <string>
#include <odb/core.hxx>
using namespace std;
#pragma db object
class Person {
private:
Person() {
}
friend class odb::access;
#pragma db id auto
unsigned long id_;
std::string email_;
std::string first_;
std::string last_;
unsigned short age_;
public:
Person(const std::string& first, const std::string& last,
unsigned short age);
/* getters */
const std::string& first() const;
const std::string& last() const;
unsigned short age() const;
const std::string& email() const;
/* setters */
void setAge(unsigned short);
void setFirst(const std::string&);
void setLast(const std::string&);
void setEmail(const std::string&);
};
#endif
then I must compile Person.hxx with odb compiler:
odb -d mysql --generate-query --generate-schema Person.hxx
and I get 4 files Person.odb.hxx, .cxx, .sql, .ixx
this is driver.cxx where I have the main program which persists objects:
#include <memory>
#include <iostream>
#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/mysql/database.hxx>
#include "Person.hxx"
#include "Person-odb.hxx"
using namespace std;
using namespace odb;
int main(int argc, char* argv[]) {
try {
auto_ptr<database> db (new odb::mysql::database (argc, argv));
unsigned long marcoID, loryID, lucaID;
/*Create some persistent Person objects */
Person marco ("Marco", "Di Nicola", 26);
Person luca ("Luca", "La Sala", 22);
Person lory ("Lorenzo", "Vinci", 24);
transaction t (db->begin());
marcoID = db->persist(marco);
lucaID = db->persist(luca);
loryID = db->persist(lory);
t.commit();
} catch (const odb::exception& e) {
cerr << e.what() << endl;
return 1;
}
}
and this is the file Person-odb.hxx
// This file was generated by ODB, object-relational mapping (ORM)
// compiler for C++.
//
#ifndef PERSON_ODB_HXX
#define PERSON_ODB_HXX
#include <odb/version.hxx>
#if (ODB_VERSION != 20200UL)
#error ODB runtime version mismatch
#endif
#include <odb/pre.hxx>
#include "Person.hxx"
#include <memory>
#include <cstddef>
#include <odb/core.hxx>
#include <odb/traits.hxx>
#include <odb/callback.hxx>
#include <odb/wrapper-traits.hxx>
#include <odb/pointer-traits.hxx>
#include <odb/container-traits.hxx>
#include <odb/no-op-cache-traits.hxx>
#include <odb/result.hxx>
#include <odb/simple-object-result.hxx>
#include <odb/details/unused.hxx>
#include <odb/details/shared-ptr.hxx>
namespace odb
{
// Person
template <>
struct class_traits< ::Person >
{
static const class_kind kind = class_object;
};
template <>
class access::object_traits< ::Person >
{
...
#include "Person-odb.ixx"
#include <odb/post.hxx>
#endif // PERSON_ODB_HXX
everything seems to work fine when I perform:
c++ -c Person-odb.cxx
c++ -c driver.cxx
but in the end when I have to link all together with:
c++ -o driver driver.o Person-odb.o -lodb-mysql -lodb
I get:
"undefined reference to `Person::Person(std::basic_string, std::allocator > const&, std::basic_string, std::allocator > const&, unsigned short)'"
What seems to be your problem is that you didn't install ODB. The Installing ODB page should get you up and running.
You see a version.hxx file, but that's an input file for the ODB compilation process, not for including in your programs.
If you did install it, find out where on your system and add that folder to your compiler include path. Your compilation command then becomes
g++ -I/usr/lib/odb/i686-linux-gnu/include -I/path/to/odb/install/include Person-odb.cxx
Following your edits, I think the issue is that you're not linking to the Person object file, only the Person-odb one.
Compile Person.cxx with
g++ -c Person.cxx
and change your linking command to
g++ -o driver driver.o Person.o Person-odb.o -lodb-mysql -lodb
and the error should be fixed.
Please note that I don't know ODB. I'm trying to figure this out from a pure C++ perspective.

C++ Simple compile error

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