c++ - 使用 **ClassObject ( C++ ) 调用公共(public)成员

标签 c++

我需要给我的公共(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/

相关文章:

c++ - Console::WriteLine() 与 cout

c++ - 当文件描述符变坏时 QSocketNotifier 会发生什么?

c++ - 如何从 perf_event_open 中的环形缓冲区中检索 PERF_RECORD_SWITCH 的调用链?

c# - 在 C++ 中正确返回 uint16_t 数组的方法

c++ - 有符号和无符号的位移位操作

c++ - 在 C++ 中,在函数定义中,参数标识符是可选的。在哪种情况下此功能可能有用?

c++ - 函数模板重载 - 偏特化

c++ - 创建 QApplication 在 4k 显示器上以高 dpi 显示时调整父窗口(非 Qt 窗口)的大小

c++ - 如何动态转换模板类

c++ - 递归调用函数对象