gpt4 book ai didi

c++:在main方法中运行一个函数

转载 作者:行者123 更新时间:2023-11-28 00:34:02 27 4
gpt4 key购买 nike

我是 C++ 的新手,正在尝试解决一些问题。我面临但不确定的问题之一是从 main() 方法调用函数时收到的超出范围的错误:

User@PC /cygdrive/c/Documents and Settings/---/folder
$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:90:9: error: ‘test01’ was not declared in this scope
test01();

test.cpp 的代码如下。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class stringStuff {
vector<string>* elements;
int frontItem;
int rearSpace;
int upperBound;

public:
stringStuff(int capacity) {
vector<string>* elements = new vector<string>(2*capacity);
frontItem = capacity;
rearSpace = capacity;
upperBound = 2 * capacity;
}

virtual void test01(){
stringStuff* sd = new stringStuff(100);
// test code here
}
};

/** Driver
*/
int main() {
test01();
}

我在这里做错了什么?

最佳答案

test01 是一个类中的成员函数。您必须实例化该类,创建一个对象才能使用它。

这将在您的 C++ 书籍中很早就涵盖,我强烈建议您在下一次尝试之前阅读更多内容。

关于c++:在main方法中运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650509/

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