2009-04-12 93 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;