tags:

views:

170

answers:

3

Hi,

I need to call my public member. The Constructor that takes 1 paramater.

This is how my code looks: // main

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 );
    }
   }
}

The error is:

'function-style cast' : illegal as right side of '->' operator

To try and resolve the issue i try two equivalent expressions:

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

Intellisense does brings up all my members and privates except the constructor.. Could this be the reason?

//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  A: 

I am assuming the following things as it is not clear from the code posted:

(1). ClassObject is defined like this: ClassType* ClassObject[/some value/10];

(2). The class definition in MyHeader.h is of ClassType and not of ClassObject.

In such a case, the following statement is the problem:

ClassObject[wrdCount] = new ClassType[x]

Here it creates 'x' number of ClassType objects. I don't think thats what you want. I guess you want to construct a ClassType object by passing const char* as the constructor parameter. If that is so you should use it like this:

ClassObject[wrdCount] = new ClassType(tmpAray);

Also note that you are assuming size of the array passed. I suggest it is better to use something like a std::string instead of raw character arrays.

Naveen
A: 

I'm not entirely clear on what you're doing, but you cannot explicitly call a constructor like that. If you have a pointer-to-a-pointer-to-a-ClassType called ClassObject, you need to do something like this to initialize it:

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);

This doesn't seem to make much sense given the code you have pasted though (since wrdCount doesn't change). It's hard to say without an exact problem description.

Volte
A: 

You need to use identifiers. The following:

ClassObject[wrdCount] = new ClassType[x] ;

tries to apply the operator[] to a class type name. What good can that do? None. Try:

ClassObject *a = new ClassType[x];

This'd create an object a of type array of size x of Classtypes. Do you need an array here -- it's upto you. If all you need is a single variable use:

ClassObject *a = new ClassType;
dirkgently
Is your second code snippet missing a *?
Nathan Kitchen
Yes, but this was before ClassObject was defined by the OP, and for some reason (I cannot recall now) I had a hunch that this is a typedef for a class pointer. Duh! I'll update.
dirkgently