error "cannot use this type here without a top-level '^'" ??

  • Thread starter Thread starter asnowfall
  • Start date Start date
A

asnowfall

I get following compilation error
C3149: 'System::String' : cannot use this type here without a top-level
'^'.

Could someone explain why I get this error?

here is the code...
ImageFileData.h
---------------------------

public value class ImageFileData
{
public:
ImageFileData();
~ImageFileData();

array<String>^ getKeywords() ;///ERRRROR
private:
array<String>^ m_sKeyword;
};


ImageFileData.cpp
----------------------------
array<String>^ ImageFileData :: getKeywords( )
{
return m_sKeyword;
}


Thanks
Ramesh
 
Hello Ramesh,
I get following compilation error
C3149: 'System::String' : cannot use this type here without a top-level
'^'.
...
array<String>^ getKeywords() ;///ERRRROR
private:
array<String>^ m_sKeyword;
};

the second line that declares m_sKeyword will also get that error.
This works better:
array<String ^>^ getKeywords();
private:
array<String ^>^ m_sKeyword;

From MSDN about the array keyword: Valid types are managed reference types
(type^), managed value types (type), and native pointers (type*).

This means that if you want to put strings in there, you have to use string
references.
--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
I get following compilation error
C3149: 'System::String' : cannot use this type here without a top-level
'^'.

Could someone explain why I get this error?

array<String^>^ and not array<String>^
 
In my experience, these are the rules. If you create a 'ref' class/struct,
array's may only consist of pointers to elements and not the instances
themselves. If you want the actually instances stored in the array, you must
create 'value' struct. These have the dis-advantage that you can't have any
methods, incuding no constructors, for them.

ref class RefClass {} ;
value struct ValueStruct{ } ;

typedef array<RefClass> NOT_Legal_Ref_Inst_Array ;
typedef array<RefClass^> Legal_Ref_Ptr_Array ;

typedef array<ValueStruct> Legal_Value_Inst_Array ;
typedef array<ValueStruct^> Legal_Value_Ptr_Array ;

[==P==]
 
Peter Oliphant said:
... If you want the actually instances stored in the array, you must
create 'value' struct. These have the dis-advantage that you can't have
any methods, incuding no constructors, for them.

That's not true. You can have methods on value types, but you must have a
no-args constructor.

-cd
 
Didn't know that. Thanx! You say you can have other methods (other than a
constructor) on value types. Can they have arguments?

Also, how would you find this info on MSDN2? Looking up the word 'value', as
you can imagine, returns so many responses it's nearly intractable to find
the one based on what we mean here by 'value'...

[==P==]
 
Peter said:
typedef array<ValueStruct^> Legal_Value_Ptr_Array ;

ValueStruct^ is not a pointer to a value, but a value boxed into an
anonymous ref class. The caret symbol always implies a garbage collected
ref class. You can't get a pointer to a value, but you can get a
reference to it: ValueStruct%.

Be careful with that, because

ValueStruct^ ptr = value;

will not create a pointer to the value, but instead it will create a
distinct boxed *copy* of it. If you start modifying "ptr", it won't have
any affect to the original value, because they're separate, unrelated
instances.

Tom
 
Thanks, Tom!

Sorry for my bad info. I still can't get use to this implicit boxing
thang... : )

[==P==]
 
Peter said:
Didn't know that. Thanx! You say you can have other methods (other
than a constructor) on value types. Can they have arguments?
Yes, there are no restrictions on value types methods.
Also, how would you find this info on MSDN2? Looking up the word
'value', as you can imagine, returns so many responses it's nearly
intractable to find the one based on what we mean here by 'value'...

When looking for this kind of information (pure language rules), I found the
ECMA specification more usefull that MSDN (I found the MSDN2 organization
concerning C++/CLI language very poor). See
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-372.pdf.
What you're looking for is explained in excrutiating details in §22.2.
This reference may not be easy to read when you're not used to it, but
you're sure to find anything language-related in it, in a rather logical
organization IMHO.

Arnaud
MVP - VC
 
Back
Top