2016-04-28 184 views
-2

我有3个文件:参数传递

file1.h

#ifndef FILE_H_INCLUDED 
#define FILE_H_INCLUDED 

#include <stdbool.h> // To prevent unknown type 'bool' error 
bool parse(char** &buffer); 

#endif 

file1.cpp

#include "file1.h" 
#include <iostream> 

using namespace std; 

bool parse(char** &buffer) { 
    buffer[0] == "test"; 
} 

而且file2.cpp包括file1.h和通话解析()用char **缓冲区;

编译时,我得到:

error: expected ';', ',' or ')' before & token 

我缺少什么?

编辑:我建立一个项目,使用原始套接字,它主要是C代码。

+3

*为了防止未知类型“布尔”错误* ...你可能使用的是C编译器而不是C++编译器 – Praetorian

+0

如果您使用的是C++编译器,则可以使用std :: string&而不是char **&来简化。 –

+0

你在使用什么系统?什么编译器?如果您使用Linux,请尝试使用g ++而不是gcc编译它。 –

回答

2

您正在使用C编译器,而不是C++编译器。

0

我想你真正要做的是通过字符数组的指针,因此,你可以在功能更改为

bool parse(char** buffer); 
0

编译器,用C语言模式,被抱怨&符号:

bool parse(char ** &buffer) 

C语言不允许在该上下文中使用&字符。

但是,它是有效的C++语法,因为函数需要一个通过引用传递的指针。如其他人所说,切换到C++编译器或告诉编译器将该文件编译为C++语言。

0

此线索:

bool parse(char** &buffer) { 
    buffer[0] == "test"; 
} 

表示“缓冲”是成为某种参照某种字符串数组的。不知道为什么它返回一个布尔值

你应该考虑(你忽略了,反正。):

// a std::vector of std::string 
typedef std::vector<std::string> StrVec_t; 

// parse can easily add individual std::strings 
// to the std::vector (from some unknown source) 
bool parse (StrVec_t& buffer) 
{ 
    buffer.push_back(std::string("test")); // tbd - where did 'test' come from? 
    return(true); // and add some meaning to this, or perhaps remove 
} 

// and is used as 
// ... 
StrVec_t myWords; 
// ... 
// ... 
(void)parse(myWords); // update myWords from some other source 
// ...    // example of how to discard bool 
+0

是的,这将需要一个C++编译器。 –