Home > Mobile >  Incompatible two void functions declaration
Incompatible two void functions declaration

Time:05-25

I have a problem about declaring two void functions in my template "Wallet" class, which are going to remove and add existing template class "CreditCard" to the vector. Compiler writes that "declaration is incompatible"

#pragma once
#include<iostream>
#include<string>
#include"CreditCard.h"
#include<vector>
using namespace std;

template<class T>
class Wallet
{
protected: 
    vector<CreditCard<T>>cards;
public:
    Wallet(vector<CreditCard<T>>cards);
    void addCreditCard(const CreditCard card);
    void removeCreditCard(const CreditCard card);
};

template<class T>
Wallet<T>::Wallet(vector<CreditCard<T>>cards) {

}

template<class T>
void Wallet<T>::addCreditCard(const CreditCard card) {
    
}

template<class T>
void Wallet<T>::removeCreditCard(const CreditCard card) {

}

CodePudding user response:

The problem is that CreditCard is a class template which is different from a class-type. So we have to specify the template argument list to make it a type.

To solve this you can specify the template arguments to CreditCard as shown below:

template<class T>
class Wallet
{
protected: 
    vector<CreditCard<T>>cards;
public:
    Wallet(vector<CreditCard<T>>cards);
//-------------------------------------vvv------------>specify template argument explicitly
    void addCreditCard(const CreditCard<T> card);
//----------------------------------------vvv------------>specify template argument explicitly
    void removeCreditCard(const CreditCard<T> card);
};
template<class T>
//--------------------------------------------vvv---------->specify template argument
void Wallet<T>::addCreditCard(const CreditCard<T> card) {
    
}

template<class T>
//-----------------------------------------------vvv---------->specify template argument
void Wallet<T>::removeCreditCard(const CreditCard<T> card) {

}

Demo.

  • Related