This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I'm learning C++ and currently experiencing weird problem with Class template.
This is my header file:
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include <list>
using namespace std;
template <int n>
class Vector {
public:
list<float> coords;
Vector();
Vector(list<float> ncoords);
};
template <int n>
Vector<n>::Vector() {
coords.assign(n, 0.0);
}
#endif
And this is my .cpp file:
#include "vector.h"
#include <list>
using std::ostream;
using namespace std;
template <int n>
Vector<n>::Vector(list<float> ncoords): coords {ncoords}{}
Everything works fine if I do Vector<2> vector;
But linker gives an error if I try to
Vector<20> vector2 { list<float>{} };
Error message
undefined reference to `Vector<20>::Vector(std::list >)'
The question is - how can I solve this problem?
Templates have to be implemented inside header files. This is due to the way linking works. Read the exhaustive answer here carefully. And the next time search before asking.
Related
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 2 years ago.
I'm trying to work with deque and template and I wanted to add others files (func.cpp and func.h) so I can declare all my function in these files and then include func.h in main.cpp but I got an some errors:
1. argument list for variable template "display" is missing in main.cpp
2.'deque': undeclared identifier in func.cpp
3.'d': undeclared identifier in func.cpp
here's the main.cpp:
#include <iostream>
#include <algorithm>
#include <deque>
#include "funcr.h"
using namespace std;
int main()
{
deque <int> d1{ 1,5,3,9 };
d1.push_front(2);
display(d1);
return 0;
}
and here's the func.cpp:
#include <iostream>
#include <deque>
template<typename T>
void display(deque<T> d)
{
for (auto e : d)
std::cout << e << std::endl;
}
and there is the func.h:
#pragma once
template<typename T>
void display(deque<T>d);
can someone help me please?
You must define display in func.h, because templated functions must be implemented in header files unless they are explicitly instantiated.
Also, you did not write using namespace std; in func.h (not that you should write it anywhere, especially headers). As a result, you need to write std::deque instead of merely deque.
So func.h should look like the following:
#include <iostream>
#include <deque>
template<typename T>
void display(const std::deque<T>& d)
{
for (auto e : d)
std::cout << e << std::endl;
}
This question already has answers here:
Unordered set of pairs, compilation error
(1 answer)
Using C++11 unordered_set in Visual C++ and clang
(1 answer)
Closed 5 years ago.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Testclass{
public:
explicit Testclass();
private:
std::unordered_map<std::vector<int>, int> world;
};
Testclass::Testclass() {
std::vector<int> temp;
world = {{temp,0}};
}
int main()
{
Testclass testclass();
return 0;
}
When I run the program above, the complier prompts that
error: implicit instantiation of undefined template 'std::__1::hash<std::__1::vector<int, std::__1::allocator<int> > >'
I know that there are some problem with the initilzation in of the class memeber "world". But I just had no idea of how to initialize it. I tried to specifiy the size of the "temp", but it did not work.
Could anyone help me solving the problem? Thanks.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I am trying to use template but it does not work, the error is : " previously declared here"
this is my Matrix.h file:
#ifndef MATRIX_H
#define MATRIX_H
template <class T>
class Matrix
{
public:
Matrix(int); // default cunstractor
private:
int rows, columns;
};
#include "Matrix.cpp"
#endif
and this is my Matrix.cpp file
#include "Matrix.h"
#include<iostream>
using namespace std;
template <class T>
Matrix<T>::Matrix(int a) // Default constructor
{
columns = a;
rows = 0;
}
and this is the main file:
#include<iostream>
#include "Matrix.h"
using namespace std;
int main()
{
Matrix<int> m1(5);
return 0;
}
I know the code seems very stupid and simple but I wrote much more, I reduce it to very simple code like this and again it doesn't work. Even I removed
#include "Matrix.cpp"
inside the Matrix.h file but still have problem.
Move the content (template definition) of your matrix.cpp file to matrix.h.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
driver.cc
#include <iostream>
#include "dynStack.h"
using namespace std;
// class definition
int main()
{
dynstack<int> c1;
c1.push(1);
cout<<"hello";
return 0;
}
dynStack.h
#include <iostream>
using namespace std;
template <class T>
class dynstack
{
public:
dynstack();
void push(T data);
};
dynStack.cc
#include "dynStack.h"
template <class T>
dynstack<T>::dynstack()
{
}
template <class T>
void dynstack<T>::push(T data)
{
return data;
}
I'm new to C++. When I run the code, it keeps giving me "undefined reference to 'dynstack::dynstack()' error. I checked include and prototype and couldn't spot the error. Could anyone help me find where I did wrong? Thank you.
Your templated class methods, unless called from within the same .cpp, need to have their implementations in the header, or an inline file included from the header.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
Undefined reference to function template when used with string (GCC)
C++ templates, undefined reference
I feel I'm missing something linking a C++ project.
I'm not sure if the problem is in the header sources or includes so I made a minimal code sample to demonstrate it.
Main module
minmain.cpp:
#include <stdio.h>
#include <vector>
#include <string>
#include "nodemin.h"
using namespace std;
int main()
{
// Blist libs are included
Node<int>* ndNew = ndNew->Root(2);
return 0;
}
Header file
nodemin.h:
#ifndef NODETEMP_H_
#define NODETEMP_H_
using namespace std;
template<class T>
class Node
{
protected:
Node* m_ndFather;
vector<Node*> m_vecSons;
T m_Content;
Node(Node* ndFather, T Content);
public:
// Creates a node to serve as a root
static Node<T>* Root(T RootTitle);
};
#endif
node module
nodemin.cpp:
#include <iostream>
#include <string.h>
#include <vector>
#include "nodemin.h"
using namespace std;
template <class T>
Node<T>::Node(Node* ndFather, T Content)
{
this->m_ndFather = ndFather;
this->m_Content = Content;
}
template <class T>
Node<T>* Node<T>::Root(T RootTitle) { return(new Node(0, RootTitle)); }
Compile line:
#g++ -Wall -g mainmin.cpp nodemin.cpp
Output:
/tmp/ccMI0eNd.o: In function `main':
/home/******/projects/.../src/Node/mainmin.cpp:11: undefined reference to`Node<int>::Root(int)'
collect2: error: ld returned 1 exit status
I tried compiling into objects but the linking still failed.
Add template class Node<int>; to nodemin.cpp:
#include <iostream>
#include <string.h>
#include <vector>
#include "nodemin.h"
using namespace std;
template <class T>
Node<T>::Node(Node* ndFather, T Content)
{
this->m_ndFather = ndFather;
this->m_Content = Content;
}
template <class T>
Node<T>* Node<T>::Root(T RootTitle) { return(new Node(0, RootTitle)); }
template class Node<int>;