I have a class 'Vector3' which is compiled successfully. It contains both non-friend and friend functions, for example, to overload * and << operators when Vector3 is the second operand. The problem is I can't link to any of the friend functions, be it operator overloaded or not. So I can confirm that the error is not specific to operator overloading. The g++ command used for linking is as follows (please also see Makefile at the end),
g++ -Wall -W -I./ -g -o main.out main.o Vector3.o
which gave the following errors,
main.cpp:7: undefined reference to `operator*(double, Vector3 const&)'
main.cpp:9: undefined reference to `mag(Vector3 const&)'
main.cpp:10: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Vector3 const&)'
Below is the relevant code in my source files. I follow the practice of making separate .hpp and .cpp for every class.
/* file Vector3.hpp */
struct Vector3 {
...
Vector3 operator*(const double k) const;
friend Vector3 operator*(const double k, const Vector3 &vec);
double magnitude() const;
friend double mag(const Vector3 &vec);
friend std::ostream& operator<<(std::ostream& output, const Vector3 &vec);
...
}
/* file Vector3.cpp */
Vector3 operator*(const double k, const Vector3 &vec) {
...
}
inline double mag(const Vector3 &vec) {
...
}
std::ostream& operator<<(std::ostream& output, const Vector3 &vec) {
...
}
/* file main.cpp */
#include "Vector3.hpp"
int main() {
Vector3 M(1, 1, 1);
M = M * 2.0; // own operator* links successfully
M = 10.0 * M; // friend operator* doesn't link
double m = M.magnitude(); // own function magnitude() links successfully
double n = mag(M); // friend function mag() doesn't link
std::cout << M; // friend operator<< doesn't link
}
Finally, this is my Makefile.
CXX = g++
CXXFLAGS = -Wall -W $(INCPATH) -g
INCPATH = -I./
OBJS = main.o Vector3.o
main.out: $(OBJS)
$(CXX) $(CXXFLAGS) -o $# $(OBJS) $(LIBPATH)
main.o: main.cpp
Vector3.o: Vector3.cpp
clean:
rm -f $(OBJS) main.out
The strangest thing is that if I include the Vector3.cpp file as well in main.cpp and then remove Vector3.o from OBJS in Makefile, the program links successfully. I cannot make sense of this. Please help me!!
The definition of friend operator* uses fp_type while the friend declaration uses double as the first parameter. This will only work as intended if fp_type is a typedef-name for double. Are you sure fp_type actually stands for double? I can't see it from the code you posted.
The problem with mag is rather obvious: you defined it as inline in .cpp file. Inline function definitions have to be visible everywhere they are used, meaning that normally they should be placed in the header file.
Related
For a exercise in C++, I must to use a protected c++ in a function which overload the operator <<.
But we know that protected function is accessible in a class or with the keyword friend if we want to define the function in a cpp file.
For the moment, I have one abstract class and the main.
I dont't have any idea how I can to fix this error, and I wanted to finish as far as possible this exercice early ;)
Card.hpp
#ifndef CARD_HPP
#define CARD_HPP
#include <string>
#include <iostream>
class Card
{
std::string name;
protected:
virtual std::ostream & toStream(std::ostream & out){out << name;return out;}
public:
Card(std::string n):name(n){}
friend std::ostream & operator<<(std::ostream & out, const Card &c);
};
#endif
Card.cpp
#include <Card.hpp>
std::ostream & operator<<(std::ostream & out, const Card &c)
{
return c.toStream(out);
}
main.cpp
#include <Card.hpp>
using namespace std;
int main()
{
Card card("montain");
cout << card << "\n";
return 0;
}
Output
clang++ -Wall -std=c++14 -c -o obj/main.o src/main.cpp -I include
clang++ -Wall -std=c++14 -c -o obj/Card.o src/Card.cpp -I include
src/Card.cpp:5:12: error: member function 'toStream' not viable: 'this' argument has type 'const Card', but function is not
marked const
return c.toStream(out);
^
include/Card.hpp:12:32: note: 'toStream' declared here
virtual std::ostream & toStream(std::ostream & out){out << name;return out;}
^
1 error generated.
makefile:16: recipe for target 'obj/Card.o' failed
make: *** [obj/Card.o] Error 1
The makefile
CC = clang++
CFLAGS = -Wall -std=c++14
HDIR = include
ABSTRACT = obj/Card.o
.PHONY: doc
compile: bin/main
./bin/main
bin/main: obj/main.o ${ABSTRACT}
${CC} ${CFLAGS} -o $# $^
obj/%.o: src/%.cpp
${CC} ${CFLAGS} -c -o $# $< -I ${HDIR}
doc:
doxygen Doxyfile
clean:
rm obj/*.o
rm bin/*
cleanDoc:
rm doc/* -rf
The problem is not that the function is protected, but that it is not const.
src/Card.cpp:5:12: error: member function 'toStream' not viable: 'this' argument has type 'const Card', but function is not
marked const
return c.toStream(out);
As the error message says, c is const due to const Card &c, and as of that you can only call const member functions on c.
So either make your toStream member function const:
virtual std::ostream & toStream(std::ostream & out) const
Or change the streaming operator so that c is not const. But that's not recommended, you should only pass as non const ref if the function is expected to change/replace the passed argument:
std::ostream & operator<<(std::ostream & out, Card &c)
I have 2 classes: a class template list.t with definition and implementation, and ticker.h and ticker.cpp, along with a driver program, main.cpp. I want to compile main.cpp to get to main.x which uses both the ticker and list class. This is my makefile so far.
# makefile for fx project
CC = g++
CFLAGS = -g -Wall -Wextra
default: main.x
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -o $# main.o list.o ticker.o
list.o: list.t
$(CC) -c list.t
ticker.o: ticker.cpp
$(CC) -c ticker.cpp list.t
main.o: list.t ticker.cpp main.cpp
$(CC) -c main.cpp ticker.cpp list.t
But on executing the command make I am getting the following error:
make
g++ -c main.cpp ticker.cpp list.t
clang: warning: list.t: 'linker' input unused
g++ -c list.t
clang: warning: list.t: 'linker' input unused
g++ -g -Wall -Wextra -o main.x main.o list.o ticker.o
clang: error: no such file or directory: 'list.o'
make: *** [main.x] Error 1
List.t - (without implementations)
#ifndef LIST_T
#define LIST_T
#include <iostream>
template <typename T>
class List
{
public:
// constructors
List();
List(T);
List(const List&);
~List();
// member functions
List& operator = (const List&);
void PushFront (const T&);
void PushBack (const T&);
T PopFront();
T PopBack();
T& Front();
T& Back();
const T& Front() const;
const T& Back() const;
size_t Size() const;
bool Empty() const;
void Clear();
void Display (std::ostream&, char = '\0') const;
//private vars
private:
class Link
{
Link (const T& t) : element_(t), nextLink_(0), previousLink_(0) {};
T element_;
Link* nextLink_;
Link* previousLink_;
friend class List<T>;
};
Link* firstLink_;
Link* lastLink_;
};
I am sure this is a simple error, and I have scoured google for this error message, but I either am not fully understanding their solutions, or they are not working for me. Either way, let me know if you have a solution to this problem, or any other comments on the quality and structure of this makefile. Also any knowledge on why my flags are apparently being unused would be greatly appreciated!
Thanks!
You need -c in the command line only when you compile source files to create object files.
Change
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -c main.o list.o ticker.o
to
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -o $# main.o list.o ticker.o
^^^^^
$# is the file name of the target of the rule.
See GNU make: Automatic Variables for more such variables.
The makefile should be the following:
CC = g++
CPPFLAGS = -g -Wall -Wextra
default: main.x
main.x: main.o ticker.o
$(CC) -o $# main.o ticker.o
ticker.o: ticker.cpp
$(CC) -c ticker.cpp $(CPPFLAGS)
main.o: list.t main.cpp
$(CC) -c main.cpp $(CPPFLAGS)
You don't compile a template file. The implementation should be inside the same file as the class declaration (putting the .hpp extension would be better also). Then the template must be included in the other files. For example, in the main, put a #include "list.t" directive.
Let's imagine that I have a class named "Myclass" for which it makes sense to overload the comparison operators. I need to put this class into a library named "libmyclass", against which I want to compile/link a program named "myprog".
Following advice, I chose to overload the comparison operators as non-member functions. I chose to declare them in the header file and define them in the implementation file, but they are not found at the linking step: "undefined reference to `operator<(...)'" (see below). What should I do to fix this?
Here is the file "myclass.h":
#ifndef MYCLASS_H
#define MYCLASS_H
#include <cstdlib>
#include <string>
using namespace std;
class Myclass {
private:
size_t x_;
public:
Myclass(void);
Myclass(const size_t & x);
const size_t & GetValue(void) const { return x_; };
};
bool operator==(const Myclass& lhs, const Myclass& rhs);
bool operator!=(const Myclass& lhs, const Myclass& rhs);
bool operator< (const Myclass& lhs, const Myclass& rhs);
bool operator> (const Myclass& lhs, const Myclass& rhs);
bool operator<=(const Myclass& lhs, const Myclass& rhs);
bool operator>=(const Myclass& lhs, const Myclass& rhs);
#endif //MYCLASS_H
Here is the file "myclass.cc":
#include "myclass.h"
Myclass::Myclass(void)
{
}
Myclass::Myclass(const size_t & x)
{
x_ = x;
}
inline bool operator==(const Myclass& lhs, const Myclass& rhs)
{
return(lhs.GetValue() == rhs.GetValue());
}
inline bool operator< (const Myclass& lhs, const Myclass& rhs)
{
return(lhs.GetValue() < rhs.GetValue());
}
inline bool operator!=(const Myclass& lhs, const Myclass& rhs){return !operator==(lhs,rhs);};
inline bool operator> (const Myclass& lhs, const Myclass& rhs){return operator< (rhs,lhs);};
inline bool operator<=(const Myclass& lhs, const Myclass& rhs){return !operator> (lhs,rhs);};
inline bool operator>=(const Myclass& lhs, const Myclass& rhs){return !operator< (lhs,rhs);};
Here is the file "myprog.c":
#include <cstdlib>
#include <iostream>
using namespace std;
#include "myclass.h"
int main (int argc, char ** argv)
{
Myclass * pt_obj1 = new Myclass(1);
Myclass * pt_obj2 = new Myclass(2);
if (*pt_obj1 < *pt_obj2)
cout << "obj1 < obj2" << endl;
else
cout << "obj1 >= obj2" << endl;
delete pt_obj1;
delete pt_obj2;
return EXIT_SUCCESS;
}
Here is the file "Makefile_lib":
PROJECT=libmyclass.a
SOURCES=myclass.cc
CC=g++
CFLAGS=-Wall -Wextra -g
OBJECTS=$(SOURCES:.cc=.o)
all: $(PROJECT)
$(PROJECT): $(OBJECTS)
ar -cvq $(PROJECT) $(OBJECTS)
.cc.o:
$(CC) $(CFLAGS) -c $< -o $#
clean:
rm -f $(OBJECTS) $(PROJECT)
Here is the file "Makefile_exe":
PROJECT=myprog
SOURCES=myprog.cc
LIB=libmyclass.a
CC=g++
CFLAGS=-Wall -Wextra -g
OBJECTS=$(SOURCES:.cc=.o)
all: $(PROJECT)
$(PROJECT): $(OBJECTS) $(LIB)
$(CC) $(OBJECTS) -L. -lmyclass
.cc.o:
$(CC) $(CFLAGS) -c $< -o $#
clean:
rm -f $(OBJECTS) $(PROJECT)
Finally, here are the commands I used and error I got:
$ make -f Makefile_lib clean
rm -f myclass.o libmyclass.a
$ make -f Makefile_lib
g++ -Wall -Wextra -g -c myclass.cc -o myclass.o
ar -cvq libmyclass.a myclass.o
a - myclass.o
$ make -f Makefile_exe clean
rm -f myprog.o myprog
$ make -f Makefile_exe
g++ -Wall -Wextra -g -c myprog.cc -o myprog.o
myprog.cc:8: warning: unused parameter ‘argc’
myprog.cc:8: warning: unused parameter ‘argv’
g++ myprog.o -L. -lmyclass
myprog.o: In function `main':
/home/me/src/myprog.cc:12: undefined reference to `operator<(Myclass const&, Myclass const&)'
collect2: ld returned 1 exit status
make: *** [myprog] Error 1
Your code contradicts itself.
When myprog.c is compiled, operator<(Myclass const&, Myclass const&) can't be inlined. The definition is nowhere to be seen.
When myclass.cc is compiled, operator<(Myclass const&, Myclass const&) isn't generated for the linker because you're promising that it will be inlined.
The solution is to either remove the inline for those functions, or move the definitions to a header so they may truly be inlined.
You should move all the inline definitions to a header file below the declarations
The inline are to be put in the .h file
or remove the inline in the .cpp file
and probably
inline bool operator!=(const Myclass& lhs, const Myclass& rhs){return !(lhs==rhs);}
is simpler than the operator form.
and no need to put an ; after a function definition.
FIXED: had the method twice in the header file
I get the following error when trying to compile my project
% make
g++ -o p4 testTree.o tree.o node.o check.o
Undefined first referenced
symbol in file
Tree::inTree(int) tree.o
ld: fatal: Symbol referencing errors. No output written to p4
collect2: ld returned 1 exit status
*** Error code 1
make: Fatal error: Command failed for target `p4'
Makefile
p4: testTree.o tree.o node.o check.o
g++ -o p4 testTree.o tree.o node.o check.o
testTree.o: testTree.cc tree.h node.h check.h
g++ -c -Wall -Werror testTree.cc
tree.o: tree.h tree.cc node.h check.h
g++ -c -Wall -Werror tree.cc
node.o: node.h node.cc check.h
g++ -c -Wall -Werror node.cc
check.o: check.h check.cc
g++ -c -Wall -Werror check.cc
clean:
rm -f *~ *.o p4
Relevant code from tree.cc and tree.h:
tree.cc
...
bool Tree::inTree(int k) const
{
return locate(k,root) != NULL;
}
...
tree.h
#ifndef TREE_H
#define TREE_H
#include "node.h"
#include "check.h"
using namespace std;
class Tree
{
private:
Node *root;
public:
Tree();
Tree(const Tree & t);
const Tree & operator=(const Tree &t);
friend ostream & operator<<(ostream &out, const Tree &t);
bool inTree(int k) const;
double & operator[](int k);
double & operator[](int k) const;
~Tree();
bool inTree(int index);
private:
Node * locate(int k, Node *rt) const;
ostream & display(ostream &out, Node *r, int dir=Node::L) const;
void add(int k, Node*&r);
void kill(Node *&rt);
void copy(Node *rt, Node *&newRt);
};
#endif
I get the feeling that it's something really simple, but I can't seem to figure it out.
The message you are getting actually comes from the linker, not from the compiler.
One of your member functions, bool Tree::inTree(int index);, is correctly declared and defined as a const member function:
// Declaration in tree.h
bool inTree(int index) const;
// Definition in tree.cc
bool Tree::inTree(int k) const
// ^^^^^
However, in tree.h you also define this non-const overload of inTree():
// Declaration in tree.h, definition (supposedly) nowhere
bool Tree::inTree(int k)
For which no definition is provided. This is what the linker complains about.
Here is your error:
bool Tree::inTree(int k) const
{
return locate(k,root) != NULL;
}
in your .h you define
bool inTree(int);
This is a difference!
This week I started to upgrade my knowledge from C to C++, I would like to overload some operators
I have a class called Matrix
#include "lcomatrix.h"
inline Matrix::Matrix(unsigned rows, unsigned cols) :
rows_(rows), cols_(cols)
{
data_ = new double[rows * cols];
}
inline Matrix::~Matrix() {
delete[] data_;
}
inline double& Matrix::operator()(unsigned row, unsigned col) {
return data_[cols_ * row + col];
}
inline double Matrix::operator()(unsigned row, unsigned col) const {
return data_[cols_ * row + col];
}
The content of lcomatrix.h is
#include <iostream>
class Matrix {
public:
Matrix(unsigned rows, unsigned cols);
double& operator()(unsigned row, unsigned col);
double operator()(unsigned row, unsigned col) const;
~Matrix(); // Destructor
Matrix& operator=(Matrix const& m); // Assignment operator
private:
unsigned rows_, cols_;
double* data_;
};
Main.cpp
#include "lcomatrix.h"
#include <iostream>
/*-
* Application entry point.
*/
int main(void) {
Matrix mx(12,12);
//std::cout << mx << std::endl;
return 0;
}
Make file:
CPPFLAGS=-I /path/lcomatrix/
EFLAGS=
all : main.o lcomatrix.o
g++ $(EFLAGS) -o main.out main.o lcomatrix.o
main.o: lcomatrix.o
g++ $(EFLAGS) $(CPPFLAGS) -c main.cpp
lcomatrix.o:
g++ $(EFLAGS) -c /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/lcomatrix.cpp
clean:
rm *.o main.out
When I try to build I receive the following link error:
make all
g++ -c /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/lcomatrix.cpp
g++ -I /home/robu/UbuntuOne/ChibiOS-RPi/lcomatrix/ -c main.cpp
g++ -o main.out main.o lcomatrix.o
main.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `Matrix::Matrix(unsigned int, unsigned int)'
main.cpp:(.text+0x2c): undefined reference to `Matrix::~Matrix()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I guess this a really stupid error, but as a beginner I couldn't figure out the solution.
Your method definitions are all inline. In order to inline a function, the compiler needs to see its definition whenever it is compiling the code that uses it.
Either put the function definitions somewhere they can be seen at the point of use - in the header, or in another file #included by Main.cpp - or don't mark them as inline.