views:

754

answers:

2

I've recently started poking around in Visual Studio 2005, and I'm mucking about in Visual C++. When I double click on a control in the designer, it opens the .h file, which I've understood to be for prototype declarations, and if I put all the guts to the functions in there, I can get my program to work, but I don't like having the code in the .h file.

However, when I put things in a .cpp file, I can't seem to access the controls at all. I get a lot of compiler errors and such, and I'm wondering what VC++ is expecting, as I'm more used to a GCC/MinGW environment.

The errors I receive the most are:

error C2228: left of '.trackBar1/.value/.etc' must have class/struct/union
error C2227: left of '->trackBar1/->Value/->etc' must point to class/struct/union/generic type

I've tried the following to access the control:

Junk::Form1::trackBar1->value
Junk::Form1::trackBar1.value
Junk::Form1->trackBar1
Junk->Form1->trackBar1
this->trackBar1->value //This is legal in the .h file, and how I can get it to work there
trackBar1->value

And a few others that are just attempts out of desperation. I've tried specifying the same namespace and everything as present in the .h file, and I still cannot seem to access the control.

I'm using Visual Studio 2005, with a Visual C++ CLR Win32 form application. The code that Visual Studio uses to create instances of the controls is:

this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());

In Dev-C++ or code::blocks, I'm used to putting the class declarations in the .h file, and then specifying the functions in the .cpp file by doing class::function, but in Visual Studio, I just cannot seem to figure out why I can't do the same, or what I'm doing horribly wrong and stupid.

Thank you.

A: 

Thanks for clarifying the question, here's some ideas

  • Ensure the .h file is included in the corresponding cpp, for example make sure that if you've defined

foo.h

class
{
...
};

be sure that foo.cpp has foo.h included, like this:

#include "foo.h"

... definitions ...
  • It looks like that within the context of your class that referencing trackBar1 should be valid, that is your last example above. If you are outside of the scope of your class, you will need to declare an instance of the enclosing class and reference its instance of trackBar.

Also, like I said before:

You need to know if your "Form1" is a value or instance of an object or a class or a namespace. If its a namespace/class latter than using :: to clarify the namespace is appropriate. From the error it looks like its likely not a namespace/class/struct. You seem to also be trying to treat it like a pointer. Is it a pointer? How is it newed?

I would simplify to experimenting with an extremely simple C++ header/cpp example. Confirm this works. Slowly copy-paste in your functionality and see at what stage it breaks. Make a hello world program is VS 2005 and add in your code.

When you find where it breaks, and its still broken, edit your question with your simplified set of sample code and we can better help you debug.

Doug T.
A: 

This may have happened because the controls were changed (i.e. double clicking). Go back to the control (in design view) and change the name. Then change it back. Then recompile.

Cheers!