我正在尝试向我们提供C ++结构的指针。我有成员MAC的struct wSignal。我将结构的指针赋予该函数。

定义结构:

struct wSignal
{
    std::string MAC;
};


使用功能:

wSignal it1 = {"22:44:66:AA:BB:CC"};
DoesPeriodExist(&it1);


函数的定义:

bool DoesPeriodExist (wSignal& s)
{
   if(it1->MAC != "")
}


错误我得到:

error: base operand of ‘->’ has non-pointer type ‘wSignal’


我究竟做错了什么?如何使用指针?抱歉,这是一个愚蠢的问题。我对指针不是很熟悉,并且正在尝试理解该概念。

最佳答案

您将参数声明为对wSignal的引用(而不是指针),在这种情况下,应将函数更改为

bool DoesPeriodExist (wSignal& s)
{
   if(s.MAC != "") ...
}


并像这样传递参数

wSignal it1 = {"22:44:66:AA:BB:CC"};
DoesPeriodExist(it1);


如果要使用指针,则应将参数类型更改为指针(更改为wSignal

bool DoesPeriodExist (wSignal* s)
{
   if(s->MAC != "")
}


并像显示代码一样传递参数

wSignal it1 = {"22:44:66:AA:BB:CC"};
DoesPeriodExist(&it1);

关于c++ - C++使用指针构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45163964/

10-12 12:32
查看更多