2017-07-18 82 views
0

我正在试图给我们一个指向C++结构的指针。我使用成员MAC来构造wSignal。我将一个结构体的指针赋给函数。C++使用指针指向函数中的结构

定义的结构:

struct wSignal 
{ 
    std::string MAC; 
}; 

使用功能:

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

错误,我得到:该功能的

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

定义

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

我做错了什么?我怎样才能使用指针?对不起,如果这是一个愚蠢的问题。我对指针不是很熟悉,而是在尝试理解这个概念。

+0

用'DoesPeriodExist调用它(IT1);';该参考已经在函数参数的定义中。 –

+0

'wSignal&'指定一个引用类型,而不是指针 – StoryTeller

+0

指向'struct wSignal'的指针是'wSignal * s'。 – Scheff

回答

4

你声明参数作为参考(以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); 
1

你给一个指针struct给需要一个参考struct功能。

这是一个需要修复的不匹配:

  • 您可以通过struct本身,DoesPeriodExist(it1),或
  • 您可以接受一个指针,bool DoesPeriodExist (wSignal* s)

第一种方法是在wSignal必须非空的情况下更可取。如果您希望允许通过NULLDoesPeriodExist,则只有第二种方法可行,因为NULL引用是不允许的。

0

你的DoesPeriodExist()定义并不指望一个指针,但到wSignal参考。正确的签名会

bool DoesPeriodExist(wSignal* s) 

因此,在您的实现基本操作数不是指针,但一个参考,这是用来与.运营商。