gpt4 book ai didi

c++ - 返回类型对象方法

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:04 25 4
gpt4 key购买 nike

如果我有这个通用方法,其中使用从 string s 派生的参数返回类型 Foo 对象:

Foo createFoo(string s)
{
int index, first, second, third, fourth, fifth;

Foo fooName(int first, int second,int third,int fourth,int fifth);
return fooName;
}

然后在 main 中,我尝试做这样的事情:

Foo newFoo = createFoo(argv[2]);

为什么编译器给我这个错误?

file.cc:30:1: error: ‘Foo’ does not name a type file.cc: In function‘int main(int, char**)’: file.cc:180:38: error: ‘createFoo’ was notdeclared in this scope

来自 Java,做这样的事情通常不会给我带来任何问题,为什么这会成为 C++ 中的问题?我该如何解决这个问题?

编辑 1:一些建议询问我的 Foo 类定义位于何处。它位于 createFoo 方法之后,所以我将 createFoo 方法移到了 Foo 类定义代码段之后并尝试编译。现在出现了一个新的错误:

file.cc: In function ‘Foo createFoo(std::string)’: file.cc:153:9:error: conversion from ‘Foo (*)(int, int, int, int, int)’ tonon-scalar type ‘Foo’ requested

最佳答案

您没有包含定义“Foo”的 header ,或者您忘记添加 using 指令以将其从定义它的命名空间中拉入。

关于c++ - 返回类型对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289512/

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