2009-04-12 90 views
0

我需要致電我的公共成員。構造函數需要1個參數。使用** ClassObject調用公共成員(C++)

這是我的代碼的外觀: //主

char tmpArray[100] = {}; 

while (!inFile.eof()) 
{ 
    for (unsigned x = 0; x < str2.length(); x++) 
    { 
    if (!isspace(str2[x]) || isspace(str2[x])) 
    { 
     tmpArray[x] = str2[x]; // prepare to supply the constructor with each word 
     ClassObject[wrdCount] = new ClassType[x] ; 
     //ClassObject[wordCount]->ClassType(tmpArray); 
    } 
    } 
} 

的錯誤是:

'功能樣式轉換':非法作爲 右側 ' - >' 操作

要嘗試解決問題,我嘗試兩個等效表達式:

/* no good */ (*ClassObject[wrdCount]).ClassType(tmpArray); 
/* no good */ (*ClassObject[wrdCount][10]).ClassType(tmpArray); 
/* combine */ ClassObject[arbitrary][values]->ClassType(tmpArray); 

智能感知確實會帶出除構造函數之外的所有我的成員和私有者。 這可能是原因嗎?

//MyHeader.h

class ClassObject 
{ 
    private: 
    const char* cPtr; 
    float theLength; 
public: 
    ClassObject(const char*); // Yes its here and saved.. 
    ClassObject(); // an appropriate default constructor 
    ~ClassObject(); 
    char GetThis(); 
    char* GetThat(); 
} 
+0

你能發佈整個代碼嗎?我不明白你是如何擁有一個ClassObject類的,並且也使用ClassObject作爲指針。 – Uri 2009-04-12 05:33:41

+0

我是否缺少某些東西,或者是「if(!isspace(str2 [x])|| isspace(str2 [x]))」總是會評估爲true? – Venesectrix 2009-04-13 15:53:22

回答

1

我假設下面的東西,因爲它是不明確從代碼貼:

(1)。 ClassObject定義如下:ClassType * ClassObject [/ some value/10];

(2)。 MyHeader.h中的類定義是ClassType而不是ClassObject。

在這種情況下,下面的語句是問題:

ClassObject[wrdCount] = new ClassType[x] 

這將創建「X」類類別對象的數目。我不認爲這是你想要的。我想你想通過傳遞const char *作爲構造函數參數來構造一個ClassType對象。如果是這樣,你應該使用這樣的:

ClassObject[wrdCount] = new ClassType(tmpAray); 

另外請注意,我們假定你是通過數組的大小。我建議最好使用類似std :: string而不是原始字符數組的東西。

0

我不完全清楚你在做什麼,但你不能明確地調用這樣的構造函數。如果你有一個指針到一個指針到A-稱爲ClassObjectClassType,你需要做這樣的事情來初始化:

ClassObject[wrdCount] = new ClassType*[x]; // create a new 'row' in the array with x columns 
for (int i = 0; i < x; ++i) // initialize each 'column' in the new row 
    ClassObject[wrdCount][i] = new ClassType(tmpArray); 

這似乎並沒有太大的意義給予代碼你已經粘貼了(因爲wrdCount不會改變)。沒有確切的問題描述很難說。

0

您需要使用標識符。以下內容:

ClassObject[wrdCount] = new ClassType[x] ; 

試圖將operator[]應用於類類型名稱。這有什麼好處?沒有。嘗試:

ClassObject *a = new ClassType[x]; 

This'd創建的Classtype的規模x的類型陣列的對象a。你需要一個數組 - 這取決於你。如果你需要的只是一個單一的變量使用:

ClassObject *a = new ClassType;