Template and deque [duplicate] - c++

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;
}

Related

C++ class function template and variable arguments problem [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 1 year ago.
I have been writing this class for logging but I haven't been fixing this problem.
mylogger.cpp
#include "../inc/mylogger.h"
MyLogger::MyLogger()
{
cout << "hi" << endl;
}
template<typename... Args>
void MyLogger::function1(Args... args)
{
printf( args...);
}
MyLogger MYLOGGER = MyLogger();
mylogger.h
#ifndef MYLOGGER_H
#define MYLOGGER_H
#include <iostream>
#include <iostream>
#include <cstdarg>
#include <string>
#include <thread>
#define LOG_WRITE MYLOGGER.function1
using namespace std;
class MyLogger{
public:
MyLogger();
template<typename... Args> void deneme(Args... args);
};
extern MyLogger MYLOGGER;
#endif
main.cpp
#include <iostream>
#include "../inc/mylogger.h"
using namespace std;
int main(int argc, char** argv)
{
LOG_WRITE("hello", 3, 4, 1);
return 0;
}
I got this error.
undefined reference to `void MyLogger::function1<char, int, int, int>(char, int, int, int)'
I don't understand. How to fix it?
You have implementation of template in other file, the compiler cannot find it and create a specific implementation for certain type. You must create file "templates.cpp" and declare a specific template there. Like on this photo:
Or you can set all your implementation of your functions from mylogger.cpp in mylogger.h

Template class does not name a type error, separated definition and declaration for header

I am attempting to create a templated vector class, but upon compilation I am receiving an error of
def.hpp:3:1: error: 'TempVector' does not name a type
I keep referring to reference material and my syntax and handling of the header file declaration and definition (.h and .hpp) seem right to me, but I can not figure out what I am overlooking.
Below is the three files I am working with, thank you.
driver.cpp:
#include <iostream>
#include <string>
#include "dec.h"
using namespace std;
int main() {
TempVector <int> v1;
cout<<"ran successfully"<<endl;
}
dec.h:
#ifndef DEC_H
#define DEC_H
#include <iostream>
#include <utility>
// Declaration of class Vector
template <typename T>
class TempVector {
public:
TempVector ();
private:
T* array;
static const unsigned int spare = 10;
};
#include "def.hpp"
#endif
def.hpp:
template <typename T>
TempVector<T>::TempVector () {
std::cout<<"ran successfully";
}

Class Template Errors

The question asked is very different from the so called duplicate post as I have specific errors and they are just asking why it needs to be in the .h file... I already have it in the header file and am getting the errors below.
Updated the files and I still get the following errors.
1 Unresolved externals (lab12.exe line 1)
unresolved external symbol "public char__thiscall Pair::geetFirst(void)" (?getFirst#?$Pair#D##QAEDXZ) referenced in function_main (lab12.obj line 1)
Pair.h
#pragma once
template <class T>
class Pair
{
private:
T theFirst;
T theSecond;
public:
/*Pair(const T& dataOne, const T& dataTwo);*/
Pair(T a, T b) {
theFirst = a;
theSecond = b;
}
T getFirst();
T getSecond();
};
Pair.cpp
#include "stdafx.h"
#include "Pair.h"
template<class T>
T Pair<T>::getFirst()
{
return theFirst;
}
template<class T>
T Pair<T>::getSecond()
{
return theSecond;
}
Main.cpp
#include "stdafx.h"
#include "Pair.h"
#include <iostream>
using namespace std;
int main()
{
Pair <char> letters('a', 'd');
cout << letters.getFirst();
cout << endl;
system("Pause");
return 0;
}
You should put all code for template class Pair into the header file.
Also, there is no need to separate method declaration from definition in header.
'Pair T::Pair' is wrong because you do not need T here.

previously declared here, C++ template: [duplicate]

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.

C++ Class expression parameter incorrect definition [duplicate]

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.