C++ trouble with extern - undefined reference - c++

main.cpp:
bool lgstatus;
User currentUser;
//...
int main(){ //... }
loginwindow.cpp:
void LoginWindow::on_cmdCreate_clicked()
{
extern bool lgstatus;
extern User currentUser;
//...
currentUser.setMail(ui->txtAccountMail->text().toStdString());
currentUser.setName(ui->txtAccountName->text().toStdString());
currentUser.setPassword(ui->txtAccountPassword->text().toStdString());
//...
lgstatus = true;
}
My class User has 3 functions. Each of them takes a string as argument. I don't know whats wrong. The compiler doesn't complain if I change lgstatus but my currenUser.
Class :
class User
{
public:
User();
User(const std::string &name, const std::string &password);
User(const std::string &name, const std::string &password, const std::string &mail);
void setName(const std::string &name);
void setMail(const std::string &mail);
void setPassword(const std::string &password);
private:
std::string user_name;
std::string user_password;
std::string user_mail;
};
The "set" functions simply pass their argument to the user_name etc. I don't think it would be necessary to show them as well.
Errors :
undefined reference to 'User::setMail(std::string const&)'
undefined reference to 'User::setName(std::string const&)'
undefined reference to `User::setPassword(std::string const&)'
What did I do wrong?

More than likely you are not doing the correct #include in your loginwindow.cpp. As a result, the compiler never finds the correct functions.

Related

C++ the default constructor of cannot be referenced -- it is a deleted function

I made class in visual studio 2022 (cpp) and when i try to create object of this class it says - C++ the default constructor of cannot be referenced -- it is a deleted function, how can i fix this error?
the class:
#pragma once
#include <string>
#include "DeviceList.h"
class User
{
private:
unsigned int id;
std::string username;
unsigned int age;
DevicesList& devices;
public:
void init(unsigned int id, std::string username, unsigned int age);
void clear();
unsigned int getID() const;
std::string getUserName() const;
unsigned int getAge() const;
DevicesList& getDevices() const;
void addDevice(Device newDevice);
bool checkIfDevicesAreOn() const;
};
the error:
User user1; //C++ the default constructor of cannot be referenced -- it is a deleted function
user1.init(123456789, "blinkybill", 17);
User user2;//C++ the default constructor of cannot be referenced -- it is a deleted function
user2.init(987654321, "HatichEshMiGilShesh", 15);
You need to add constructor to User class for initializing devices member reference.
public:
User(DevicesList& dvs) : devices(dvs){}
after that, create user1 with DeviceList instance
DevicesList devices;
User user1(devices);

Undefined reference to static member of class

I am about to learn basic OOP operations in C++ and now I encountered a problem with static members of class. I try to build simple card game. I create classes Rules, Deck and Card.
My Deck class taking rules and some constants from Rules class and then do something. I'm only use Deck::createDeck(); in main function, nothing else. When I try to compile the code I get in result error:
/usr/bin/ld: CMakeFiles/CardGame.dir/Sources/Rules.cpp.o: in function Rules::getSuits[abi:cxx11]()':
/home/bartosz/CLionProjects/CardGame/Sources/Rules.cpp:12: undefined reference toRules::suits[abi:cxx11]'
/usr/bin/ld: CMakeFiles/CardGame.dir/Sources/Rules.cpp.o: in function Rules::getRanks[abi:cxx11]()':
/home/bartosz/CLionProjects/CardGame/Sources/Rules.cpp:16: undefined reference toRules::ranks[abi:cxx11]'
collect2: error: ld returned 1 exit status
But I belive that static members (suits and ranks) are corectlly initialized, so why compiler doesn't see this variables?
My code:
Rules.h
#ifndef CARDGAME_RULES_H
#define CARDGAME_RULES_H
#include <string>
class Rules {
public:
static std::string suits[4];
static std::string ranks[13];
public:
static std::string * getSuits();
static std::string * getRanks();
};
#endif //CARDGAME_RULES_H
Rules.cpp
#include "../Headers/Rules.h"
std::string suits[4] = {"Diamonds", "Hearts", "Spades", "Clubs"};
std::string ranks[13] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
std::string * Rules::getSuits() {
return suits;
}
std::string * Rules::getRanks() {
return ranks;
}
Deck.h
#ifndef CARDGAME_DECK_H
#define CARDGAME_DECK_H
#include "Card.h"
class Deck {
private:
Card * cards;
Deck();
public:
static void createDeck();
void shuffle();
void dealCards();
};
#endif //CARDGAME_DECK_H
Deck.cpp
#include "../Headers/Deck.h"
#include "../Headers/Rules.h"
Deck::Deck() {
}
void Deck::createDeck() {
std::string * ranks = Rules::getRanks();
std::string * suits = Rules::getSuits();
// some operations
}
void Deck::shuffle() {
}
void Deck::dealCards() {
}
In Rules.cpp, you don't define the static members Rules::suits and Rules::ranks, but rather introduce 2 new global variables.
In order for the static definition to work, you need to specify the fully qualified name, e.g. Rules::suits.
Use a constant expression constexpr
In the header Rules.h file:
constexpr std::array<std::string_view, 4> suits = {"Diamonds", "Hearts", "Spades", "Clubs"};

class and pass string as argument to method

How to pass a string to a method in a class?
code
class Txtbin{
private:
std::string input;
std::string output = "output.png";
void error();
public:
Txtbin();
void run();
};
Txtbin::Txtbin(){
}
void Txtbin::error(const char* str){
throw std::runtime_error(str);
}
void Txtbin::run(){
if(input == ""){
error("Input file not defined");
}
}
error
# g++ -std=c++11 txtbin.cpp -o txtbin `pkg-config opencv --cflags --libs`
txtbin.cpp:30:6: error: prototype for ‘void Txtbin::error(const char*)’ does not match any in class ‘Txtbin’
void Txtbin::error(const char* str){
^
txtbin.cpp:14:8: error: candidate is: void Txtbin::error()
void error();
^
As others mentioned, you are declaring void error(); but defining void error(const char* str);. Put const char* str parameter in the declaration too, inside the class.
prototype for ‘void Txtbin::error(const char*)’
does not match any in class ‘Txtbin’
You're trying to define Txtbin's void error(const char*) function, but it does not have one.
candidate is: void Txtbin::error()
It does, however, declare a void error() function, without the parameter. Since you actually use that parameter in the implementation, you probably want to add it to its declaration.
Like others have said, void error() requires no parameter. However later you create void error(const char* str) which has a parameter.
class Txtbin{
private:
string input;
string output = "output.png";
public:
Txtbin();
void error(const char*); //This is what you need.
/* void error(); THIS IS WHAT YOU HAD */
void run();
};
void Txtbin::error(const char* str)
{
//Whatever
}

netbeans multiple definition; first defined in _nomain.o error on building C++ simple test

I've created three C++ simple tests in tests folder in my projects directory. When I created the first C++ simple test and built it there was no problem but when I create the second or third, innumerable errors listed below are generated
build/DebugDynamic/GNU-Linux-x86/ClientSocket.o: In function `Socket::is_valid() const':
/home/gowtham/workspace/base/ClientSocket.cpp:8: multiple definition of `ClientSocket::ClientSocket(std::string, int, Socket::SOCKET_TYPE, std::string, std::string, std::string)'
build/DebugDynamic/GNU-Linux-x86/ClientSocket_nomain.o:/home/gowtham/workspace/base/ClientSocket.cpp:8: first defined here
The linker command is
g++ -g -O0 -o build/DebugDynamic/GNU-Linux-x86/tests/TestFiles/f3 build/DebugDynamic/GNU-Linux-x86/tests/tests/sizeInfo.o build/DebugDynamic/GNU-Linux-x86/ClientSocket_nomain.o build/DebugDynamic/GNU-Linux-x86/FFJSON_nomain.o build/DebugDynamic/GNU-Linux-x86/JPEGImage_nomain.o build/DebugDynamic/GNU-Linux-x86/ServerSocket_nomain.o build/DebugDynamic/GNU-Linux-x86/Socket_nomain.o build/DebugDynamic/GNU-Linux-x86/logger_nomain.o build/DebugDynamic/GNU-Linux-x86/myconverters_nomain.o build/DebugDynamic/GNU-Linux-x86/mycurl_nomain.o build/DebugDynamic/GNU-Linux-x86/mystdlib_nomain.o build/DebugDynamic/GNU-Linux-x86/myxml_nomain.o build/DebugDynamic/GNU-Linux-x86/ClientSocket.o build/DebugDynamic/GNU-Linux-x86/FFJSON.o build/DebugDynamic/GNU-Linux-x86/JPEGImage.o build/DebugDynamic/GNU-Linux-x86/ServerSocket.o build/DebugDynamic/GNU-Linux-x86/Socket.o build/DebugDynamic/GNU-Linux-x86/logger.o build/DebugDynamic/GNU-Linux-x86/myconverters.o build/DebugDynamic/GNU-Linux-x86/mycurl.o build/DebugDynamic/GNU-Linux-x86/mystdlib.o build/DebugDynamic/GNU-Linux-x86/myxml.o -lxml2 -lpthread -lssl -lcrypto -lz
netbeans is including a duplicate object file _nomain.o for every object file in the project.
ClientSocket.h
#ifndef CLIENTSOCKET_H
#define CLIENTSOCKET_H
#include "Socket.h"
class ClientSocket : public Socket {
public:
class AftermathObj {
public:
void* (*aftermath)(void* aftermathDS, bool isSuccess);
void* aftermathDS;
std::string payload;
std::string* payloadPTR;
std::string error;
int __flags;
pthread_t t;
ClientSocket* cs;
AftermathObj() {
};
~AftermathObj() {
};
};
ClientSocket();
ClientSocket(std::string host, int port, Socket::SOCKET_TYPE socketType = Socket::DEFAULT, std::string trustedCA = "", std::string privatecert = "", std::string privatekey = "");
std::string host;
int port;
void reconnect();
void disconnect();
bool send(const std::string s, int __flags) const;
bool send(const std::string* s, int __flags) const;
bool send(const std::string s) const;
virtual ~ClientSocket();
const ClientSocket& operator <<(const std::string&) const;
void asyncsend(std::string payload, AftermathObj* after_math_obj);
void asyncsend(std::string* payload, AftermathObj* aftermath_obj);
const ClientSocket& operator >>(std::string&) const;
private:
//Socket* soc;
static void* socsend(void*);
struct soc_send_t_args {
std::string s;
void* (&aftermath)(void* aftermathDS);
void* aftermathDS;
};
pthread_key_t socket_thread_key;
};
#endif

declaration of static function outside of class is not definition

I am getting this error when I compile with GCC:
error: declaration of 'static int utils::StringUtils::SplitString(const std::string&, const std::string&, std::vector<std::basic_string<char> >&, bool)' outside of class is not definition
Code:
Header:
namespace utils
{
/*
* This class provides static String utilities based on STL library.
*/
class StringUtils
{
public:
/**
* Splits the string based on the given delimiter.
* Reference: http://www.codeproject.com/Articles/1114/STL-Split-String
*/
static int SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties = true );
};
};
Source:
namespace utils
{
int StringUtils::SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties );
{
....
}
}
Take the semi-colon off the end of the definition in your source file! Copy-paste error =)
I believe you need to lose that semicolon in your source file. Should be:
namespace utils
{
int StringUtils::SplitString( const std::string& input,
const std::string& delimiter,
std::vector<std::string>& results,
bool includeEmpties ) // <--- No more semi-colon!
{
....
}
}