GTEST - ASSERT_EQ and EXPECT_EQ compile error - c++

I have been trying to use the assertion of ASSERT_EQ and EXPECT_EQ,
ASSERT_EQ(queue_->size(),6);
even using this
ASSERT_EQ(6,6);
However, I encountered this compiler error and everywhere I searched no one seems to be facing
this problem.
/tmp/cczZfMaq.o: In function testing::AssertionResult testing::internal::EqHelper<false>::Compare<char, char>(char const*, char const*, char const&, char const&)':
/usr/include/gtest/gtest.h:1485: undefined reference totesting::AssertionResult testing::internal::CmpHelperEQ(char const*, char const*, char const&, char const&)'
collect2: error: ld returned 1 exit status
make: * [priority_queue_test.out] Error 1
Full Test File :
//Include the class which you would like to test
#include "priority_queue.h"
//Include the google c++ test framework
#include
#include
#include
//use the namespace for the testing class
using namespace algorithms;
using namespace containers;
namespace{
//Test Fixture Class
//multple tests in a test case shared common objects and subroutines
class PriorityQueueTest : public ::testing::Test{
protected:
PriorityQueueTest(){
}
virtual ~PriorityQueueTest(){
}
virtual void SetUp(){
queue_ = new PriorityQueue(1000);
}
virtual void TearDown(){
}
PriorityQueue *queue_=NULL;
};
TEST_F(PriorityQueueTest,CheckAllMemberInitSuccessfully){
//ASSERT is a critcal assertion which will fail the test program
ASSERT_TRUE(queue_!=NULL) capacity()==1000) capacity();
}
TEST_F(PriorityQueueTest,CheckIfItemsAreEnqueueCorrectly){
//test cases
queue_->Enqueue(0,0);
queue_->Enqueue(1,1);
queue_->Enqueue(6,6);
queue_->Enqueue(5,5);
queue_->Enqueue(3,3);
queue_->Enqueue(2,2);
ASSERT_EQ('a','a');
}
}//namespace
int main(int argc, char *argv[]){
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}

Related

Undefined reference with JsonBox functions

I am trying to follow the penguin programmer text-based rpg guide (http://www.penguinprogrammer.co.uk/rpg-tutorial/introduction/). I am working on the item class and I am having some undefined reference errors with JsonBox functions.
#include "Item.hpp"
#include "Entity.hpp"
#include "JsonBox/include/JsonBox.h"
//#include "EntityManager.hpp"
Item::
Item(std::string _id, std::string _name, std::string _description)
: Entity(_id) {
this->SetName(_name);
this->SetDescription(_description);
}
Item::
Item(std::string _id, JsonBox::Value& _v, EntityManager* _mgr)
: Entity (_id) {
this->Load(_v, _mgr);
}
void
Item::
Load(JsonBox::Value& _v, EntityManager* _mgr) {
JsonBox::Object o = _v.getObject();
this->SetName(o["name"].getString());
this->SetDescription(o["description"].getString());
return;
}
Then I am compiling this main.
//#include "EntityManager.cpp"
#include "Item.cpp"
#include "JsonBox/include/JsonBox.h"
int main() {
return 0;
}
And getting this error
/tmp/cc1sZXwe.o: In function `Item::Load(JsonBox::Value&, EntityManager*)':
main.cpp:(.text+0x286): undefined reference to `JsonBox::Value::getObject() const'
main.cpp:(.text+0x2d4): undefined reference to `JsonBox::Value::getString() const'
main.cpp:(.text+0x35c): undefined reference to `JsonBox::Value::getString() const'
/tmp/cc1sZXwe.o: In function `std::pair<std::string const, JsonBox::Value>::~pair()':
main.cpp: (.text._ZNSt4pairIKSsN7JsonBox5ValueEED2Ev[_ZNSt4pairIKSsN7JsonBox5ValueEED5Ev]+ 0x18): undefined reference to `JsonBox::Value::~Value()'
/tmp/cc1sZXwe.o: In function `std::pair<std::string const, JsonBox::Value>::pair(std::pair<std::string const, JsonBox::Value> const&)':
main.cpp: (.text._ZNSt4pairIKSsN7JsonBox5ValueEEC2ERKS3_[_ZNSt4pairIKSsN7JsonBox5ValueEEC5 ERKS3_]+0x3b): undefined reference to `JsonBox::Value::Value(JsonBox::Value const&)'
/tmp/cc1sZXwe.o: In function `std::pair<std::string const, JsonBox::Value>::pair<std::string&&, 0ul>(std::tuple<std::string&&>&, std::tuple<>&, std::_Index_tuple<0ul>, std::_Index_tuple<>)':
main.cpp: (.text._ZNSt4pairIKSsN7JsonBox5ValueEEC2IIOSsEILm0EEIEIEEERSt5tupleIIDpT_EERS6_I IDpT1_EESt12_Index_tupleIIXspT0_EEESF_IIXspT2_EEE[_ZNSt4pairIKSsN7JsonBox5ValueE EC5IIOSsEILm0EEIEIEEERSt5tupleIIDpT_EERS6_IIDpT1_EESt12_Index_tupleIIXspT0_EEESF _IIXspT2_EEE]+0x47): undefined reference to `JsonBox::Value::Value()'
Inside your main you include Item.cpp source file, changing your main to this might help you:
//#include "EntityManager.cpp"
#include "Item.hpp"
#include "JsonBox/include/JsonBox.h"
int main() {
return 0;
}
Including source files is usually a bad practice, and can cause many problems, you can read more about Including .cpp files

C++11 vector of shared_ptr template

I am trying to call the following function template:
template<typename T>
bool select(const std::string& ddbbName,
const std::string& sql,
std::vector<std::shared_ptr<T>>& vResultSet,
SqlErrorInfo& errorInfo);
which is defined in a class whose name is SQLite3Manager. In the following code this "select" method does nothing (a part from returning "true"). I have tried to simplify the problem description, because the problem seems to be related to the way I am calling/using/defining that method.
So the main.cpp code is:
main.cpp
#include <iostream>
#include "ES.h"
#include "SQLiteMgr.h"
int main(int argc, const char * argv[])
{
// To get an instance of the singleton
Cucut::SQLite3Manager& _sqliteMgr = Cucut::SQLite3Manager::getInstance();
std::string ddbbName("Cucut.db");
std::string sql("SELECT * FROM ES");
std::vector<std::shared_ptr<Cucut::ES>> vspEs;
Cucut::SqlErrorInfo sqlErrorInfo;
// Call the template method for <Cucut::ES> using the instance of the singleton
bool result = _sqliteMgr.select<Cucut::ES>(ddbbName, sql, vspEs, sqlErrorInfo);
return result;
}
but I get the following link error in Xcode 5:
Undefined symbols for architecture x86_64:
"bool Cucut::SQLite3Manager::select<Cucut::ES>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<Cucut::ES>, std::__1::allocator<std::__1::shared_ptr<Cucut::ES> > >&, Cucut::SqlErrorInfo&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The full code is:
ES.h file:
#ifndef __TestSharedPtr__ES__
#define __TestSharedPtr__ES__
#include <iostream>
namespace Cucut
{
class ES
{
public:
ES();
const unsigned int& getId() const;
void setId(const unsigned int& id);
private:
unsigned int _id;
};
}
#endif /* defined(__TestSharedPtr__ES__) */
ES.cpp file:
#include "ES.h"
namespace Cucut
{
ES::ES() :
_id(0)
{
}
const unsigned int& ES::getId() const
{
return _id;
}
void ES::setId(const unsigned int& id)
{
_id = id;
}
}
SQLiteMgr.h file:
#ifndef __TestSharedPtr__SQLiteMgr__
#define __TestSharedPtr__SQLiteMgr__
#include <iostream>
#include <vector>
#include <memory>
namespace Cucut
{
struct SqlErrorInfo
{
int rc;
std::string description;
};
class SQLite3Manager
{
private:
SQLite3Manager();
SQLite3Manager(const SQLite3Manager& rs);
SQLite3Manager(SQLite3Manager&& rs);
SQLite3Manager& operator = (const SQLite3Manager& rs);
SQLite3Manager& operator = (SQLite3Manager&& rs);
public:
static SQLite3Manager& getInstance();
template<typename T>
bool select(const std::string& ddbbName,
const std::string& sql,
std::vector<std::shared_ptr<T>>& vResultSet,
SqlErrorInfo& errorInfo);
};
}
#endif /* defined(__TestSharedPtr__SQLiteMgr__) */
And finally the SQLiteMgr.cpp file:
#include <memory>
#include <vector>
#include "SQLiteMgr.h"
namespace Cucut
{
SQLite3Manager::SQLite3Manager()
{
}
SQLite3Manager& SQLite3Manager::getInstance()
{
static SQLite3Manager instance;
return instance;
}
template<typename T>
bool SQLite3Manager::select(const std::string& ddbbName,
const std::string& sql,
std::vector<std::shared_ptr<T>>& vResultSet,
SqlErrorInfo& errorInfo)
{
return true;
}
}
Do not be distract with the name "SqliteMgr" because in the aforementioned example I have removed any kind of reference to sqlite3 in order to simplify the problem; so, it seems that I am not calling or defining the method "select" in the correct way because I get the aforementioned link error.
Any help will be appreciated. Thanks in advance.
Function template definitions must always be in the header file so that code can be generated at the point of instantiation (here in main). If you don't do this, the compiler will expect you to manually instantiate the template, which is why there is a linker error. Move the body of the select function to SQLiteMgr.h and it will work.

boost thread error <unresolved overloaded function type>

I'm working on an optimisation project and have decided to try threads to increase the speed of my code. the format of the code is:
Main.cpp:
int main(int argc, char **argv) {
B *b = new B(argv[1]);
b->foo();
delete b;
return EXIT_SUCCESS;
}
B.cpp:
#include B.hpp
B::B(const char *filename) { .... }
B::task1(){ /*nop*/ }
void B::foo() const {
boost::thread td(task1);
td.join();
}
B.hpp:
#include <boost/thread.hpp>
class B{
public:
void task1();
void foo();
}
however when I try to compile this code, I get an error at boost::thread td(task1), saying:
error: no matching function for call to 'boost::thread::thread(<unresolved overloaded function type>)'
not entirely sure what the problem is and I've tried hacking away to no success. Any help is appreciated!
edit: new error
B.o: In function 'B::b() const':
B.cpp:(.text+0x7eb): undefined reference to 'vtable for boost::detail::thread_data_base'
B.cpp:(.text+0x998): undefined reference to 'boost::thread::start_thread()'
B.cpp:(.text+0x9a2): undefined reference to 'boost::thread::join()'
B.cpp:(.text+0xa0b): undefined reference to 'boost::thread::~thread()'
B.cpp:(.text+0xb32): undefined reference to 'boost::thread::~thread()'
B.o: In function 'boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::cmf0<void, B>, boost::_bi::list1<boost::_bi::value<B const*> > > >::~thread_data()':
B.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi4cmf0Iv4BEENS2_5list1INS2_5valueIPKS6_EEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi4cmf0Iv4BEENS2_5list1INS2_5valueIPKS6_EEEEEEED5Ev]+0x8): undefined reference to 'boost::detail::thread_data_base::~thread_data_base()'
B::task() is a member function, so it takes an implicit first parameter of type B*. So you need to pass an instance to it in order to use it in a boost::thread.
void B::foo() const {
boost::thread td(&B::task1, this); // this is a const B*: requires task1() to be const.
td.join();
}
But since B::foo() is a const method, you would have to make B::task1() a const method too:
class B {
void task1() const:
}

c++ compile error Undefined symbols for architecture x86_64

I wrote a class named UserScore:
//header
using namespace std;
class UserScore{
public:
UserScore(const int &user_id, const int &rating);
private:
int _user_id;
int _rating;
};
//cpp
#include "UserScore.h"
UserScore::UserScore (const int &user_id, const int &rating):
_user_id(user_id),
_rating(rating)
{
}
The compile command is:
g++ src/UserScore.cpp -o obj/UserScore.o
But why this simple thing won't compile?
The error is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
You need to include the -c option to the compilation line if you want to generate an object file, otherwise the compiler assumes you are trying to build an executable and will complain if you don't have a main() method (which is happening here).
To make an object file which you will link later to a code file that has a "main()" method in it you need to use
g++ -c src/UserScore.cpp -o obj/UserScore.o
Which is what I think you are trying to do.
Alternatively you just need to add a main function to your code and then you can make an executable.
//header
using namespace std;
class UserScore{
public:
UserScore(const int &user_id, const int &rating);
private:
int _user_id;
int _rating;
};
//cpp
#include "UserScore.h"
UserScore::UserScore (const int &user_id, const int &rating):
_user_id(user_id),
_rating(rating)
{
}
int main()
{
return 0;
}

function invoking undefined reference

hi every one I have this class with those headers
class WuManber
{
public:
WuManber( void );
virtual ~WuManber( void );
void Initialize( const vector<const char *> &patterns,
bool bCaseSensitive = false, bool bIncludeSpecialCharacters = false, bool bIncludeExtendedAscii = false );
when I try try to create an instance of WuManber and invoke Initialize I get the following error:
/tmp/ccx19Os5.o: In function main': Test.cpp:(.text+0x8d): undefined
reference toWuManber::WuManber()' Test.cpp:(.text+0xbc): undefined
reference to WuManber::Initialize(std::vector<char const*,
std::allocator<char const*> > const&, bool, bool, bool)'
Test.cpp:(.text+0xc8): undefined reference toWuManber::~WuManber()'
Test.cpp:(.text+0x115): undefined reference to `WuManber::~WuManber()'
collect2: ld returned 1 exit status
int main(int argc, char* argv[])
{
Parser CustomParserEx;
CustomParserEx.open("/home/abdullah/Project IDS/rules");
WuManber WmAlgorithm;
WmAlgorithm.Initialize(CustomParserEx.patterns,true,true,true);
}
so any hints of what I am doing wrong
You didn't implement the three methods of WuManber, or you didn't include the implementations where the linker can find them.