取消引用导致错误:")"标记之前的预期主表达式
Dereference causes error: expected primary-expression before ')' token
当我在函数中使用解引用作为参数时,预处理器会吐出一个错误。我相信括号前的*会引起编译器的歧义。有什么办法可以绕过这个吗?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main ()
{
char *in = NULL;
char *out = NULL;
getline(cin,in*);//error
out=system(in*);//error
printf(out);
return 0;
}
错误在标记的行上。谢谢你!
解除对in
的引用写成*in
,而不是in*
。(此外,即使修复了这个问题,您的程序仍然无法工作,因为您试图解引用NULL
,并且getline
的第二个参数将具有错误的类型。char*
字符串不像你想象的那样工作
getline
只适用于c++字符串(不是C风格的字符串)。c++字符串可以根据读取的数据量分配内存。
还有其他的函数可以读取C字符串,但是你必须预先分配你想要的内存量,并指定你已经分配了多少内存量。一般来说,没有理由这样做,因为c++字符串版本更简单,更不容易出错。
同时,避免包含c风格的标准头(即以.h
结尾)并避免使用指针。system
返回int类型,而不是string类型。
的例子:
#include <iostream> // cin, cout
#include <string> // string
#include <cstdlib> // system
int main()
{
std::string s;
std::getline( std::cin, s );
int system_result = std::system( s.c_str() );
std::cout << system_result << "n";
}
相关文章:
- 函数返回的 rvalue 引用(表达式)是 xvalue - 但没有标识?
- 仅通过引用捕获的 lambda 表达式是否保证不会抛出?
- C++在构造函数处用表达式初始化引用
- C++ 解决方法:"从类型"B*"的表达式初始化类型"C*&"的引用无效"
- 获取表达式的类型(包括引用C++)
- lambda 表达式中引用捕获的 constexpr 变量和非显式捕获的 constexpr 变量之间的区别
- 通过引用传递表达式与通过引用传递变量
- 常量表达式中引用类型的变量
- 将函数的引用和 lambda 表达式作为参数传递时有什么区别?
- 在给定表达式的情况下返回对类型的引用时出错:“cond ?*这个 : 投掷()'
- 引用初始化和常量表达式
- 为什么返回非引用类型的函数表达式被认为是prvalues而不是xvalues
- C++:从类型"int*"的表达式初始化类型"const int*&"的引用无效
- Boost正则表达式:链接时未定义的引用
- 关于解引用运算符是否在表达式中产生对象对值的定义不明确
- 错误:从类型"testGetter* const"的表达式初始化类型"const testGetter*&"的引用无效
- 尝试使用 uint*& 作为常量单元*&失败:从类型"uint8_t"的表达式初始化类型"const uint8_t*&"的引用无效
- C++常量引用允许从表达式进行更改
- 为什么将表达式强制转换为对函数的右值引用是左值
- 错误:从类型"const int"的表达式初始化类型"int"的引用无效