M
M
Monstr19862018-09-06 13:57:52
C++ / C#
Monstr1986, 2018-09-06 13:57:52

When inheriting a class template, I get error C2143, what is the reason?

I inherit the Typeinfo class from TypeSize.
TypeSize.h

using namespace std;

template <class T1>
class TypeSize
{
public:
  TypeSize(T1 value);

  void DataTypeSize();

protected:
  T1 value;
};

TypeInfo.h
using namespace std;

template <class T1>
class TypeInfo : public TypeSize<T1> // Здесь получаю C2143 синтаксическая ошибка: отсутствие "," перед "<"
{
public:
  TypeInfo(T1 value);
  
  void ShowTypeName();
};

stdafx.h
#pragma once

#include "targetver.h"
#include <tchar.h>
#include <iostream>

#include "TypeInfo.h"
#include "TypeSize.h"

Tell me, what could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-09-07
@Monstr1986

I don’t know why you need this, without understanding the syntax and other basic things, climb into templates. You are all wrong there.

SizeType.h
#pragma once
#include <iostream>

template<typename T>
class TypeSize
{
public:
  TypeSize(T v) : value{ v } {};
  ~TypeSize() {};
  void dataTypeSize();
protected:
  T value;
};

template<typename T>
void TypeSize<T>::dataTypeSize()
{
  std::cout << "size: " << sizeof(value) << std::endl;
}

TypeInfo.h
#pragma once
#include <typeinfo> // std::typeid()
#include "TypeSize.h"

template<typename T>
class TypeInfo : public TypeSize<T>
{
public:
  TypeInfo(T v) : TypeSize<T>(v) {};
  ~TypeInfo() {};
  void showTypeInfo();
};

template<typename T>
void TypeInfo<T>::showTypeInfo()
{
  std::cout << "type: " << typeid(this->value).name() << std::endl;
}

main.cpp
#include "TypeInfo.h"

int main()
{
  int a{ 5 };
  TypeInfo<int> infInt(a);
  infInt.dataTypeSize();
  infInt.showTypeInfo();
  std::getchar();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question