简体   繁体   English

将类作为函数参数传递时,C ++“不是类型”错误

[英]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对象传递给Sendo名为connect的函数成员。

device.h 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 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 : g ++编译器向我显示sendo.h文件和函数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. 您可能必须bool connect(Device<int> target) (例如),或使Sendo成为模板类。 Anyway some template parameter is required for declaring variables of type Device . 无论如何,声明一些Device类型的变量都需要一些模板参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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