Bob Shannon
Bob Shannon

Reputation: 648

c++: Running a function in main method

I'm new to C++ and trying to figure some things out. One of the problems that I am facing and not sure of is an out of scope error that I am receiving when calling a function from the main() method:

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

The code for the test.cpp is below.

#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();
}

What am I doing wrong here?

Upvotes: 0

Views: 49

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

test01 is a member function in a class. You have to instantiate the class, creating an object, to use it.

This will be covered fairly early on in your C++ book, which I strongly recommend reading more before your next attempt.

Upvotes: 3

Related Questions