gpt4 book ai didi

c++ - 错误 : 'xxx' is not a type

转载 作者:行者123 更新时间:2023-11-30 00:46:33 25 4
gpt4 key购买 nike

<分区>

在这个完整的代码中:

class foo
{
public:
foo(const int pin);
};

class bar {
public:
// Constructor
bar(const int dataPin) : dataPin_ (dataPin) { }

private:
const int dataPin_;
foo myFoo_ (dataPin_); // instance of foo
};

int main (void)
{
return 0;
}

使用 g++ 4.8.4 我得到错误:

g++ -Wall -c "test.cpp" (in directory: /home/nick/Development)
test.cpp:14:17: error: ‘dataPin_’ is not a type
foo myFoo_ (dataPin_); // instance of foo
^
Compilation failed.

使用 clang 3.4-1ubuntu3 我得到:

test.cpp:14:17: error: unknown type name 'dataPin_'
foo myFoo_ (dataPin_); // instance of foo
^
1 error generated.

为什么这里需要一个类型?这是试图创建 foo 的实例作为 bar 的类变量。上面直接声明了变量 dataPin_


如果我将有错误的行改成这样,它会干净地编译:

    foo myFoo_ (int dataPin_);  // instance of foo

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