1

I have a Feature class which has a public vehicle_type in it and I have 3 candidates for vehicle_type variable which are; Car,Truck and Motor classes. When I call constructor for Feature class, well I want my program to get which vehicle type was called with constructor automatically. I actually tried to use Templates for the problem but it didn't work. So, can someone tell me how to achieve this with or without using Templates.

This is my Feature.h file:

#ifndef FEATURE_H
#define FEATURE_H

#include <string>
#include "../includes/Car.h"
#include "../includes/Truck.h"
#include "../includes/Motor.h"


template <typename T>
class Feature{
    public:
        Feature();
        Feature(int w, std::string b, std::string clr, double p, T v_t);
        T vehicle_type;
        int wheels;
        std::string brand;
        std::string color;
        double price;

};


#endif

This is my Feature.cpp file:

# include "../includes/Feature.h"

template <typename T> 
Feature::Feature(int w, std::string br, std::string clr, double p, T v_t){wheels = w; brand=br; color = clr; price=p;  vehicle_type = v_t; }
Feature::Feature(){}

and lastly, this below is the error occurs when I hover my mouse on Feature constructor in the Feature.cpp file.

name followed by '::' must be a class or namespace name

Feel free to ask for more information. Thanks.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Ali Ziya ÇEVİK
  • 186
  • 1
  • 1
  • 8
  • 1
    (A) wrong syntax for defining a template class constructor (B) template method definitions must be defined in a header file (C) class template deduction is new as of like C++17 or C++20. What version are you using? – Mooing Duck May 13 '21 at 18:34
  • @MooingDuck I'm using c++17. – Ali Ziya ÇEVİK May 13 '21 at 18:35
  • 3
    Have you considered using inheritance / polymorphism for this, rather than a template? – Paul Sanders May 13 '21 at 18:36
  • @PaulSanders No I haven't actually. I thought maybe, I could use 3 constructors for each 3 different classes but I think this is not the best idea. – Ali Ziya ÇEVİK May 13 '21 at 18:39
  • 1
    With inheritance, you can have the constructor of each derived class pass the appropriate vehicle type to the parent constructor (and drop the template completely). That would be my choice. – Paul Sanders May 13 '21 at 18:41
  • @AliZiyaÇEVİK: You can't put the implementation of templates in a .cpp file in most cases. – Nicol Bolas May 13 '21 at 18:43
  • Are `Car`, `Truck`, and `Motor` actual class types that you instantiate and pass to `Feature`? Or are they values in an `enum` instead? Your constructor code suggests they are distinct types, but the name `vehicle_type` implies an `enum`. What is the use-case for `Feature` exactly? – Remy Lebeau May 13 '21 at 21:48
  • @RemyLebeau They are all actual classes inherited from a ```Vehicle```class. I still couldn't find a way to achieve this. I tried inheritence as @Paul Sanders said but couldn't find a way it works. – Ali Ziya ÇEVİK May 14 '21 at 05:25

1 Answers1

0

It looks like you just need to supply the template declaration and the fix the syntax:

template <typename T> 
Feature<T>::Feature(int w, std::string br, std::string clr, double p, T v_t);

template<class T>
Feature<T>::Feature();
David G
  • 94,763
  • 41
  • 167
  • 253