gpt4 book ai didi

Why does MSVC think that the input parameter to this function is of type int, although it's not?(为什么MSVC认为这个函数的输入参数是int类型的,尽管它不是?)

转载 作者:bug小助手 更新时间:2023-10-25 12:46:44 25 4
gpt4 key购买 nike



I tried to learn about the implicit this pointer and its uses, but I can't seem to understand why my compiler (MSVC) keeps talking about int as the type for the function printcmon(), instead of cmon.

我试图了解隐式this指针及其用法,但我似乎不能理解为什么我的编译器(MSVC)一直将int作为函数printcmon()的类型,而不是cmon。


void printcmon(const cmon* e);

class cmon
{
public:
int x, y;
cmon(int x, int y) {
this->x = x;
this->y = y;
printcmon(this);
}

int getX() const {
const cmon* e = this;
std::cout << e->x << std::endl;
return 0;
}
};

void printcmon(const cmon* e) {
//stuff
}

Errors:

错误:


error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error: missing ',' before '*'
error C2664: 'void printcmon(const int)': cannot convert argument 1 from 'cmon *' to 'const int'

message : There is no context in which this conversion is possible
message : see declaration of 'printcmon'
message : while trying to match the argument list '(cmon *)'

更多回答

You get downvotes because you have posted the text as a link to a picture.

你得到了反对票,因为你把文本作为一个图片的链接发布了。

No its not. See this post on it.

不,不是的。请看上面的这篇文章。

Voted to reopen. This question does not need debugging details. It's quite clear what's being asked.

投票决定重新开张。这个问题不需要调试细节。很清楚被问到的是什么。

@Joel I see. If the error screenshot is replaced with plain text with real line numbers, it'll be a good question.

@乔尔,我明白了。如果将错误屏幕截图替换为具有真实行号的纯文本,这将是一个很好的问题。

@Evg -- need better training for high-rep folks to not bully newcomers. For example, don't say "why do you think X" when you mean "X is incorrect". And (this is harder than it sounds) instead of "you do X..." say "the code does X".

@EVG--需要对高代表人进行更好的培训,不要欺负新来的人。例如,当你的意思是“X是不正确的”时,不要说“你为什么认为X”。而且(这比听起来更难)而不是“你做X……”说“代码做X”。

优秀答案推荐

There is no class cmon before first line void printcmon(const cmon* e);
Add a declaration:

在第一行空的printcmon(const cmon*e)之前没有cmon类;添加一个声明:


class cmon;
void printcmon(const cmon* e);

class cmon
{
public:
int x, y;
cmon(int x, int y) {
this->x = x;
this->y = y;
printcmon(this);
}

int getX() const {
const cmon* e = this;
std::cout << e->x << std::endl;
return 0;
}
};

void printcmon(const cmon* e) {
//stuff

}

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com