I need some Help with this small code that includes re-def of operator and Template
But I keep getting this error:
error C2955 use of class template requires template argument list
#include<iostream>
using namespace std;
template <class T>
class student
{
friend int operator+(student &other1,student &other2);
public:
student(T g);
private:
int grade;
};
template <class T>
student<T>::student(T g)
{
grade=g;
}
void main()
{
student<int> s1(40),s2(90);
int sum;
sum=s1+s2;
cout<<"average="<<sum/2.0<<endl;
}//main
template <class T>
int operator+(student &other1,student &other2)
{
return other1.grade+other2.grade;
}
You need to say
template<class T>
int operator+(student<T> a, student<T> b)
...
You were missing the T
Related
I have a header file,
class CListEx
{
public:
CListEx();
~CListEx();
void InitList();
void AddList(char * msg);
private:
template <typename T>
struct MyList {
T data;
int num;
};
};
Now in Cpp file,
void CListEx::InitList()
{
MyList *my = new MyList();
}
And this gives error,
E0441 argument list for class template "CListEx::MyList" is missing
Error C2514 'CListEx::MyList': class has no constructors
Any ideas?
What I TRIED:
I tried below approach and that also gives errors.
template <typename T >MyList *my = new template <typename T >MyList();
The error message gives a very clear reason: argument list for class template is missing. Meaning, you should write some code like:
void CListEx::InitList()
{
MyList<int> *my = new MyList<int>();
}
Read up on templates for example at: https://www.programiz.com/cpp-programming/templates
Is this what you are trying to do?
class CListEx
{
public:
template <typename T>
void InitList();
private:
template <typename T>
struct MyList {
T data;
int num;
};
};
template <typename T>
void CListEx::InitList()
{
MyList<T> *my = new MyList<T>();
}
int main()
{
CListEx cle;
cle.InitList<int>();
}
Or are you trying to do this?
template <typename T>
class CListEx
{
public:
void InitList();
private:
struct MyList {
T data;
int num;
};
};
template <typename T>
void CListEx<T>::InitList()
{
MyList *my = new MyList();
}
int main()
{
CListEx<int> cle;
cle.InitList();
}
Beware of the memory leak. Preferably use std::unique_prt from <memory>.
How can I create a functor (in templates) that get void or int
template <class T>
Int TEMP::operator()(T s)
{
...
}
Thanks
Probably by not using templates:
template<class T> int TEMP<T>::operator()(int s)
{
...
}
template<class T> int TEMP<T>::operator()()
{
...
}
I'm sure there is a very easy answer, but I can't figure it out. I have written a templated class, but I want to pass that class by reference in a class function that isn't templated. Heres what I have. I get a bunch of errors. All I need to do is figure how to format the way to insert templated class into function, but I'm at a lost. Thank you and sorry if the code doesn't really help you out.
#include <iostream>
using namespace std;
template <typename T>
class Foo {
public:
Foo();
insert(const T& Item)
//And other function, just examples
};
class noFoo(){
void test(Foo <T>& foo);
int i;
int j;
int k
};
template <typename T>
void noFoo::test(Food <T>& foo)}
cout << "hi";
}
int main() {
Foo<char> wr;
test(wr);
return 0;
}
Make test a function template. I also corrected loads of syntax errors for you (class noFoo()?), removed unnecessary code, and ran clang-format for indentation.
#include <iostream>
template <typename T>
class Foo {};
class noFoo
{
public:
template <typename T>
void test(Foo<T> &);
};
template <typename T>
void noFoo::test(Foo<T> &)
{
std::cout << "hi\n";
}
int main()
{
Foo<char> wr;
noFoo{}.test(wr);
}
Since your question is tagged d, here the same code in D.
import std.stdio;
class Foo(T) {};
class noFoo
{
public:
void test(T)(Foo!(T))
{
writeln("hi");
}
};
void main()
{
auto wr = new Foo!char;
(new noFoo).test(wr);
}
I have this simple code below, a template with 2 type parameters. If I declare my class with the same type (like BidirectionalMap<int,int>), I receive an error:
int BidirectionalMap<T,S>::operator [](T) const' : member function already defined or declared
Here's my template code:
template <class T, class S>
class BidirectionalMap{
int count(T t){
return 1;
}
int count(S s){
return 1;
}
};
The error you got is normal, because after substitution you have
template <>
class BidirectionalMap<int, int>
{
int count(int t){ return 1; }
int count(int s){ return 1; } // Duplicated method
};
To solve that, you may provide partial specialization:
template <class T>
class BidirectionalMap<T, T>
{
int count(T t) { return 1; }
};
In C++20, you might use requires to "discard" methods:
template <class T, class S>
class BidirectionalMap
{
int count(T t) requires(!std::is_same<T, S>::value) { /*..*/ }
int count(S s) requires(!std::is_same<T, S>::value) { /*..*/ }
int count(T t) requires( std::is_same<T, S>::value) { /*..*/ }
};
I'm new to C++ and would like to get some help with fixing the following template function code without removing the function fl()
template<type T>
class Test
{
int f1(T* x);
};
template< T>
int Test::f1(T* x)
{
return 5:
};
You have numerous syntax errors, but I guess your main issue is you need Test<T>::f1 instead of Test::f1:
//typename, not type
template<typename T>
class Test
{
int f1(T* x);
};
// forgot typename
template<typename T>
int Test<T>::f1(T* x)
//need ^^^
{
return 5;
}
//^ no semicolon
The correct syntax would be
template<typename T>
class Test
{
int f1(T* x);
};
template<typename T>
int Test<T>::f1(T* x)
{
return 5;
};
Note the keyword specifying the template argument T is either typename or class