类型错误和指针混淆
Type error and pointer confusion
我正在尝试学习指针,但我遇到了这个错误。我需要更改头文件类Request吗?为什么我会犯这样的错误?
cannot convert `req' from type `Request' to type `Request *'
错误发生在以下几行:
//Store necessary information in a Request object for each request.
Request req(url, request, 1);
Request *reqq = req; //req points to the object
list->Append(reqq);
代码:
void
ClientThread(int request)
{
const int sz = 50;
char url[sz];
FILE *fp = fopen("url.txt", "r");
if (!fp)
printf(" Cannot open file url.txt!n");
else {
int pos = 0;
char c = getc(fp);
while (c != EOF || pos == sz - 1) {
if (c == 'n') {
url[pos] = ' ';
serve(url);
pos = 0;
//Store necessary information in a Request object for each request.
Request req(url, request, 1);
Request *reqq = req; //req points to the object
list->Append(reqq);
}
else {
url[pos++] = c;
}
c = getc(fp);
}
fclose(fp);
}
}
我的request.h文件包括以下内容:
class Request
{
public:
//constructor intializes request type
Request(char *u, int rqtID, int rqtrID);
char *url;
int requestID;
int requesterID;
}
您需要在此处使用操作员的地址:
Request *reqq = &req; //req points to the object
// -------------^
注意,在这种情况下&
并不意味着引用。
如果操作数是某种类型T的左值表达式,则运算符&创建并返回类型为T*的prvalue。
使用&req
放置req
的引用。指针类型接受指针值,而不是对象。
Request *reqq = &req; //req points to the object
相关文章:
- 为什么我在空指针错误(链表)中获取成员访问权限
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 为什么我得到 munmap_chunk():无效的指针:错误
- 成员模板函数指针错误
- 删除指针时,未分配被释放的 C++ 指针错误
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- C++数组指针错误无法将“int*”转换为“int**”
- C++ 通过函数指针错误调用成员函数
- 从 BST 中删除元素(指针错误)
- 随机生成的多维布尔指针错误
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 抽象类和唯一指针 c++ 错误
- 返回函数指针错误而不带类型定义
- 练习中出现空指针错误
- 指针错误导致的分段错误(简单..)
- free():C++中的指针错误无效
- 使用布尔值数组"表达式必须具有指向对象类型的指针"错误C++
- 指针错误的C 数组