undefined reference to class Card - c++

I am trying to compile my project, but the compiler is showing the following errors every time there is a call to Card FigureCard Class :
bin/FigureCard.o: In function `FigureCard::FigureCard()':
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:5: undefined reference to `Card::Card()'
bin/FigureCard.o: In function `FigureCard::FigureCard(Figure, Shape)':
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:6: undefined reference to `Card::Card(Shape)'
bin/FigureCard.o: In function `FigureCard::~FigureCard()':
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:10: undefined reference to `Card::~Card()'
bin/FigureCard.o: In function `FigureCard::toString[abi:cxx11]()':
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:26: undefined reference to `Card::getCapitalShape[abi:cxx11]()'
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:28: undefined reference to `Card::getCapitalShape[abi:cxx11]()'
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:30: undefined reference to `Card::getCapitalShape[abi:cxx11]()'
/users/studs/bsc/2016/dalitroz/workspace/ass1/src/FigureCard.cpp:31: undefined reference to `Card::getCapitalShape[abi:cxx11]()'
bin/FigureCard.o:(.rodata._ZTV10FigureCard[_ZTV10FigureCard]+0x28): undefined reference to `Card::getShape()'
bin/FigureCard.o:(.rodata._ZTI10FigureCard[_ZTI10FigureCard]+0x10): undefined reference to `typeinfo for Card'
this is my makefile:
CC = g++
CFLAGS = -g -Wall -Weffc++ -std=c++11
LFLAGS = -L/usr/lib
# All Targets
all: reviiyot
# Tool invocations
# Executable "reviiyot" depends on the files reviiyot.o and run.o.
reviiyot: bin/reviiyot.o bin/Card.o bin/Hand.o bin/Deck.o bin/Player.o bin/NumericCard.o bin/FigureCard.o
#echo 'Building target: reviiyot'
#echo 'Invoking: C++ Linker'
$(CC) -o bin/Card.o bin/Hand.o bin/Deck.o bin/Player.o bin/reviiyot.o bin/NumericCard.o bin/FigureCard.o $(LFLAGS)
#echo 'Finished building target: reviiyot'
#echo ' '
# Depends on the source and header files
bin/reviiyot.o: src/reviiyot.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/reviiyot.o src/reviiyot.cpp
bin/Card.o: src/Card.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/Card.o src/Card.cpp
bin/NumericCard.o: src/NumericCard.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/NumericCard.o src/NumericCard.cpp
bin/FigureCard.o: src/FigureCard.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/FigureCard.o src/FigureCard.cpp
bin/Hand.o: src/Hand.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/Hand.o src/Hand.cpp
bin/Deck.o: src/Deck.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/Deck.o src/Deck.cpp
bin/Player.o: src/Player.cpp
$(CC) $(CFLAGS) -c -Iinclude -o bin/Player.o src/Player.cpp
#Clean the build directory
clean:
rm -f bin/*
this is the H file (Card.h) :
#ifndef CARD_H_
#define CARD_H_
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
enum Shape {
Club,
Diamond,
Heart,
Spade
};
enum Figure {
Jack,
Queen,
King,
Ace
};
class Card {
private:
Shape shape;
public:
Card();
Card(Shape myShape);
virtual string toString() = 0; //Returns the string representation of the card "<value><shape>" exp: "12S" or "QD"
virtual ~Card();
virtual Shape getShape();
virtual int getType(int n) const =0;
string getCapitalShape();
};
class FigureCard : public Card {
private:
Figure figure;
public:
FigureCard();
FigureCard(Figure myFigure, Shape myShape);
virtual ~FigureCard();
string toString() override;
Figure getFigure() const ;
int getType(int n)const override;
};
#endif
Class.cpp:
#include "../include/Card.h"
using namespace std;
Card::Card(): shape(Club){
}
Card::Card(Shape myShape) : shape(myShape){
}
//////check
Card:: ~Card(){}
Shape Card::getShape()
{
return shape;
}
//////check
string Card:: getCapitalShape(){
if(shape== Club)
return "C";
else if (shape == Diamond)
return "D";
else if (shape == Heart)
return "H";
else return "S";
}
string Card::toString() {
return getCapitalShape();
}
and FigureCard:
#include "../include/Card.h"
FigureCard::FigureCard (): Card(), figure(Jack){}
FigureCard::FigureCard(Figure myFigure, Shape myShape) : Card(myShape), figure(myFigure) {
}
FigureCard:: ~FigureCard()
{}
Figure FigureCard::getFigure() const
{
return figure;
}
int FigureCard::getType(int n) const
{
return figure+1+n;
}
string FigureCard ::toString() {
if(figure== Jack)
return "J"+Card::getCapitalShape();
else if (figure == Queen)
return "Q"+Card::getCapitalShape();
else if (figure == King)
return "K"+Card::getCapitalShape();
else return "A"+Card::getCapitalShape();
}
would love the help, thanks :)

You're linking bin/Hand.o bin/Deck.o bin/Player.o bin/reviiyot.o bin/NumericCard.o bin/FigureCard.o into bin/Card.o
Change:
$(CC) -o bin/Card.o bin/Hand.o bin/Deck.o bin/Player.o bin/reviiyot.o bin/NumericCard.o bin/FigureCard.o $(LFLAGS)
to
$(CC) -o reviiyot bin/Card.o bin/Hand.o bin/Deck.o bin/Player.o bin/reviiyot.o bin/NumericCard.o bin/FigureCard.o $(LFLAGS)

Related

Compilation error when including eigen library

I'm currently using eigen library, and this is the only file that I include eigen:
Kraftwerk2.cpp:
#include "Kraftwerk2.h"
Kraftwerk2::Kraftwerk2(int n){ //n: num instances
Connectivity_mat.resize(n,n);
}
int Kraftwerk2::Parse_Inst_Name(string s){ //input: String("M12"), output: int 12
return stoi(s.substr(1));
}
void Kraftwerk2::Generate_Connectivity_matrix(unordered_map<string, net> map){
for(auto& it : map){
int n =it.second.net_pin.size();
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
Connectivity_mat(i,j) = Connectivity_mat(i,j) +1;
}
}
}
}
void Kraftwerk2::Print_Mat(){
IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
cout << Connectivity_mat.format(CleanFmt);
}
Kraftwerk2.h:
#include <iostream>
#include <Eigen/Core>
#include "module.h"
using namespace std;
// unordered_map<string, instance> instances;
// unordered_map<string, net> nets;
using namespace Eigen;
class Kraftwerk2{
public:
Kraftwerk2(int);
MatrixXd Connectivity_mat;
int Parse_Inst_Name(string);
void Generate_Connectivity_matrix(unordered_map<string, net>);
void Print_Mat();
};
And I use makefile for compiling:
(I'm pretty new to makefile, so if there's something I can improve, please tell me)
(Eigen is installed in the working directory ./eigen)
# CC and CFLAGS are varilables
CC = g++
CFLAGS = -c
OPTFLAGS = -O2
DBGFLAGS = -g -D_DEBUG_ON_
# make all
all : bin/partition
#echo -n "make complete!"
# optimized version
bin/partition: main_opt.o FM.o partition.o module.o Kraftwerk2.o
$(CC) $(OPTFLAGS) main_opt.o FM.o partition.o module.o Kraftwerk2.o -o bin/partition
main_opt.o: src/main.cpp src/FM_alg.h src/partition.h src/module.h src/Kraftwerk2.h
$(CC) -I ./eigen $< -o $#
FM.o: src/FM_alg.cpp src/FM_alg.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
partition.o: src/partition.cpp src/partition.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
Kraftwerk2.o: src/Kraftwerk2.cpp src/Kraftwerk2.h
$(CC) ./eigen $(CFLAGS) $(OPTFLAGS) $< -o $#
module.o: src/module.cpp src/module.h
$(CC) $(CFLAGS) $(OPTFLAGS) $< -o $#
# clean all the .o and executable files
clean:
rm -rf *.o lib/*.a lib/*.o bin/*
However, when I make, the terminal seems to output some error that comes form the library itself(???
g++ -I ./eigen src/main.cpp -o main_opt.o
In file included from ./eigen/Eigen/Core:269,
from src/Kraftwerk2.h:2,
from src/main.cpp:4:
./eigen/Eigen/src/Core/util/IndexedViewHelper.h:69:23: error: declaration of ‘template<class first> constexpr Eigen::Index Eigen::internal::first(const first&)’ shadows template parameter
69 | EIGEN_CONSTEXPR Index first(const T& x) EIGEN_NOEXCEPT { return x.first(); }
| ^~~~~
./eigen/Eigen/src/Core/util/IndexedViewHelper.h:68:10: note: template parameter ‘first’ declared here
68 | template<typename T>
| ^~~~~~~~
make: *** [makefile:15: main_opt.o] Error 1
I would be so appreciated if anybody can tell me what's wrong with this.

The executable test file is not running the tests

I have created a project:
// func.h :
#ifndef FUNCS_H_
#define FUNCS_H_
int addInts(int i, int j);
#endif /* FUNCS_H_ */
// func.cpp
#include "func.h"
int addInts(int i, int j) {
return i + j;
}
// main.cpp
#include "func.h"
#include <iostream>
int main() {
std::cout << addInts(5, 7) << std::endl;
return 0;
}
//makefile
OBJS := \
main.o \
func.o
CXX := g++
CXX_FLAGS := -Wall -Werror -Wextra -std=c++11
all: main_prj
main_prj: $(OBJS)
$(CXX) $(OBJS) -lm -o main
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $*.cpp -o $*.o
clean:
rm -f main $(OBJS)
And I created also a test (boost test) for that function:
// test/main_test.cpp
#define BOOST_TEST_MODULE main_test
#include <boost/test/included/unit_test.hpp>
//____________________________________________________________________________//
// FIXTURES ...
//____________________________________________________________________________//
// test/addInts/addInts_test.cpp
#include <boost/test/unit_test.hpp>
#include "../../func.h"
BOOST_AUTO_TEST_CASE(addInts_5_7) {
int addInts_5_7 = 5 + 7;
BOOST_CHECK_EQUAL(addInts_5_7, addInts(5, 7));
}
// test/makefile
OBJS_TEST := \
main_test.o \
addInts/addInts_test.o \
../func.o
CXX_TEST := g++
CXX_FLAGS_TEST := -Wall -Werror -Wextra -std=c++11
all_test: main_test_prj
main_test_prj: $(OBJS_TEST)
$(CXX_TEST) $(OBJS_TEST) -lm -o main_test
-include $(OBJS_TEST:.o=.d)
%.o: %.cpp
$(CXX_TEST) -c $(CXX_FLAGS_TEST) $*.cpp -o $*.o
clean_test:
rm -f main_test $(OBJS_TEST)
Now, the make commands work, so they clean all the created files, or create the o files and the executables. When I run the main file, the output is correct: 12 ; but when I run the main_test file, the output is a little strange:
Running 1 test case...
*** No errors detected
I would expect the output with running tests and OK/KO, or pass/not pass... I cannot figure what am I doing wrong. Can anyone help me, please?
You have one test.
The output says that one test was run, and that no errors were found.
This output appears to be as documented.
There is no problem here.
Though, sadly, the documentation on how to change this output is rather lacking…

MongoDB example failure to compile with different flags

So I had a really strange compiler result after compiling the snippet provided by the MongoDB Quickstart (New Driver).
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
The code compiles perfectly with
c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)
but failed to compile when I made a makefile and added some flags
CXX=g++
FLAGS=-v -Wall --std=c++11 -I/usr/local/include
EXENAME=matchingengine
SOURCES=main.cpp
OBJECTS=main.o
all: $(EXENAME)
$(EXENAME): $(OBJECTS)
$(CXX) $(FLAGS) $(SOURCES) -o $(EXENAME)
clean:
rm *o $(EXENAME)
I keep getting
main.cpp:3:10: fatal error: 'bsoncxx/builder/stream/document.hpp' file not found
Where i have adjusted the -I include directory pointing to the place where bsoncxx is located. Please help me on this compiler issue.
I solved it
Makefile
CXX=g++
CFLAGS=-c --std=c++11 -Wall -I/usr/local/include/mongocxx/v0.3 -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v0.3 -I/usr/local/include/libbson-1.0
LDFLAGS=-L/usr/local/lib -lmongocxx -lbsoncxx
SOURCES=main.cpp
OBJECTS=main.o
EXECUTABLE=matchingengine
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(OBJECTS) -o $(EXECUTABLE) $(LDFLAGS)
$(OBJECTS): $(SOURCES)
$(CXX) $(CFLAGS) -c $(SOURCES)

Undefined referece to class constructor

I'm getting the following error:
> /tmp/ccYbdvB8.o: In function `main':
> /home/caleb/Documents/dev/cs438/tote2/mp2/manager.cpp:5: undefined
> reference to `TCPConnection::TCPConnection(char const*, char const*)'
> collect2: error: ld returned 1 exit status make: *** [manager] Error 1
I've included the header to the class I'm trying to initialize.. so I'm thinking maybe it's my makefile? I'm pretty new to writing custom makefiles...
cpp:
#include "tcpcon.h"
const string TCPConnection::Client = "client";
const string TCPConnection::Server = "server";
TCPConnection::TCPConnection(const char* t, const char* p) : target(t), port(p)
{ }
h:
class TCPConnection{
public:
TCPConnection(const char *target, const char *port);
main:
#include "tcpcon.h"
int main()
{
TCPConnection *TCPCon = new TCPConnection("localhost", "7777");
cout << "Hi\n";
return 0;
}
makefile:
CC=g++
CCOPTS=-Wall -Wextra -g
OBJS = tcpcon.o
TARGETS = manager
.PHONY: all clean
$(TARGET) : $(OBJS)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
all: $(OBJS) $(TARGETS)
clean:
rm -f $(TARGETS) $(OBJS)
%: %.cpp
$(CC) $(CCOPTS) -o $# $<
It turns out that the issue may have been related to a couple of things:
Use of $(TARGET) instead of $(TARGETS) in one place
Did not include the main file (manager.cpp) to the OBJS list
My updated makefile is below:
CC=g++
CCOPTS=-Wall -Wextra -g
OBJS = manager.o tcpcon.o
TARGETS = manager
.PHONY: all clean
$(TARGETS) : $(OBJS)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
all: $(TARGETS) $(OBJS)
clean:
rm -f $(TARGETS) $(OBJS)
%: %.cpp
$(CC) $(CCOPTS) -o $# $<

c++ undefined reference to destructor

Sorry about this, but I am re-opening this. After sorting the eigen errors, this cropped right back up again. Exactly the same code exactly the same error. (well, the compiler found the eigen headers this time.) So, same question:
I have searched for destructor c++ and undefined reference to no avail. However I am pretty sure this is a fairly simple slip on my part.
Error:
/tmp/ccDsaJ9v.o: In function `main':
geomSetup.cpp:(.text+0x5ab): undefined reference to `SASAGeometry::~SASAGeometry()'
geomSetup.cpp:(.text+0x5cd): undefined reference to `SASAGeometry::~SASAGeometry()'
collect2: ld returned 1 exit status
make: *** [geomTest] Error 1
SASAGeometry.h:
class SASAGeometry
{
public:
//methods
SASAGeometry() ;
int makeFromFiles(char *, char *, char *) ;
~SASAGeometry() ;
//globals
std::list<E......};
SASAGeometry.cpp
SASAGeometry::SASAGeometry(){}
int SASAGeometry::makeFromFiles(char * xyz_file, char * dat_file, char * atoms_file)
{
sasa_transformMatrix basisMaker ;
list<Vect...
...
}
SASAGeometry::~SASAGeometry(){}
geomTest.cpp
int main(int argv, char * argc[])
{
list<Vector3d>::iterator listIterator ;
char * xyz_file = argc[1] ;
char * dat_file = argc[2] ;
char * atoms_file = argc[3] ;
SASAGeometry geomMaker ;
int geomErr....
...
return 0 ;
}
makefile :
# compiler choice
CXX = g++
# executable path
BIN = .
# include paths (or lack thereof :p)
INCLUDE = -I .
# compilation flags
CXXFLAGS = -pipe # -O6
# linking flags
LFLAGS = -lm
# object declarations
GeomTest_OBJS = geomTest.o SASAGeometry.o
geomTest_source = SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h geomSetup.cpp
SASAGeometry.o : SASAGeometry.cpp SASAGeometry.h sasa_transformMatrix.cpp sasa_transformMatrix.h
geomTest.o : geomSetup.cpp SASAGeometry.o
# compile
geomTest : $(GeomTest_OBJS) makefile
$(CXX) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CXXFLAGS) $(geomTest_source) $(LFLAGS)
$(CXX) $(LIBS) $(INCLUDE) $(CXXFLAGS) -o $(BIN)/geomTest geomTest.o SASAGeometry.o $(LFLAGS)
clean : \rm *.o *~ p1
My INCLUDE and LIBS flags are all ok, all other methods in the SASAGeometry class are quite happily defined.
Thanks in advance.
You are not including SASAGeometry.cpp in makefile along with geomTest.cpp. makefile should be something like this:
geomTest : $(GeomTest_OBJS) makefile
$(CC) -o geomTest.o -o SASAGeometry.o $(LIBS) $(INCLUDE) $(CFLAGS) $(geomTest_source) $(LFLAGS)
$(CC) $(LIBS) $(INCLUDE) $(CFLAGS) -o $(BIN)/geomTest geomTest.o $(LFLAGS)