简体   繁体   中英

C++ “is not a type” error when passing class as a function parameter

I am trying to pass a class that has a template to a function as an object that is in another class. In other words, I want to pass the object of Device to a function member of Sendo called connect .

device.h

#ifndef DEVICE_H
#define DEVICE_H

template<class T> class Device{
public:
    T target;
    std::string address;
    Device(std::string address) : address(address){
        target    = new T;
    }
    bool coonect(){
        target.connect(address)){
            connected = true;
        }
    }
};

#endif // DEVICE_H

sendo.h

#ifndef SENDO_H
#define SENDO_H

#include "device.h"
class Sendo{
    public:
        Sendo(){
        }
        bool connect(Device target){
            target.connect();
        }
};

#endif // SENDO_H

Problem

g++ compiler shows me an error in the sendo.h file and function connect :

`Device` is not a type

How can i solve this problem?

You may have to either bool connect(Device<int> target) (for example), or make Sendo a template class. Anyway some template parameter is required for declaring variables of type Device .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM