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

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.

Related

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

Template and deque [duplicate]

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

expected ')' before object. class does not name a type [duplicate]

This question already has answers here:
C++: Has Not Been Declared
(2 answers)
Closed 4 years ago.
I don't know what is going on. I included file guards in header files but still get the error.
these are my classes:
Car.h
#ifndef CAR_H
#define CAR_H
#include "Color.h"
class Car
{
public:
Car(Color a);
void printInfo();
private:
Color carColor;
};
#endif
Car.cpp
#include "Car.h"
#include <iostream>
using namespace std;
Car::Car(Color a)
: carColor(a)
{
}
void Car::printInfo() {
cout << "the car is ";
carColor.printColor();
}
Color.h
#ifndef COLOR
#define COLOR
#include "Car.h"
#include <iostream>
using namespace std;
class Color
{
public:
Color(string c);
void printColor();
private:
string colorr;
};
#endif // COLOR
Color.cpp
#include "Color.h"
Color::Color(string c)
: colorr(c)
{
}
void Color::printColor() {
cout << colorr;
}
Edit:
guys I didn't insult to anybody. imagine you wrote lots of words and stackoverflow gives you error about question and you edit but still the same and again edit and ... this happens again and again. what should I write more ?? the question is simple and short even don't need to explain it.
For starters, you should remove #include "Car.h" from Color.h.
It creates an unnecessary circular include, and the compiler hits Car(Color a) before it knows that Color is a class.
You also need to include the header <string> to output a string to cout.
Next time, maybe don't insult the people who are helping you.

Why do I keep getting this "undefined reference" error? [duplicate]

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.

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.