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

c++: Running a function in main method

本文关键字:运行 函数 方法 main c++      更新时间:2023-10-16

我是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();

测试的代码.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++书的早期介绍,我强烈建议你在下次尝试之前阅读更多内容。