2014-02-08 85 views
0

我是C++的新手,並試圖弄清楚一些事情。之一的,我面對並不能確定問題是出從主()方法調用函數時,我接收範圍誤差的:C++:在main方法中運行函數

[email protected] /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(); 
} 

我在這裏做錯了什麼?

+1

關於術語的注意事項:'main'不是方法AKA的成員函數。 – hyde

回答

3

test01是類中的成員函數。你必須實例化這個類,創建一個對象來使用它。

這將在您的C++書中相當早的時候介紹,我強烈建議您在下次嘗試之前閱讀更多內容。