我需要给我的公共(public)成员(member)打电话。采用 1 个参数的构造函数。
这是我的代码的样子: //主要
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 );
}
}
}
错误是:
'function-style cast' : illegal as right side of '->' operator
为了尝试解决这个问题,我尝试了两个等效的表达式:
/* no good */ (*ClassObject[wrdCount]).ClassType( tmpArray );
/* no good */ (*ClassObject[wrdCount][10]).ClassType( tmpArray );
/* combine */ ClassObject[arbitrary][values]->ClassType( tmpArray );
Intellisense 会调出除构造函数之外的所有成员和私有(private)对象。 会不会是这个原因?
//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( );
}
最佳答案
我假设以下内容,因为从发布的代码中看不清楚:
(1)。 ClassObject 定义如下:ClassType* ClassObject[/some value/10];
(2)。 MyHeader.h 中的类定义是 ClassType 而不是 ClassObject。
在这种情况下,以下语句是有问题的:
ClassObject[wrdCount] = new ClassType[x]
在这里它创建了“x”个 ClassType 对象。我不认为那是你想要的。我猜您想通过传递 const char* 作为构造函数参数来构造一个 ClassType 对象。如果是这样,你应该像这样使用它:
ClassObject[wrdCount] = new ClassType(tmpAray);
另请注意,您假设传递的数组的大小。我建议最好使用 std::string 之类的东西而不是原始字符数组。
关于c++ - 使用 **ClassObject ( C++ ) 调用公共(public)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/741306/