Lutzee

Hello everybody i wonder if you can fill a hole in my understanding of programming windows.

I am trying to develop a very basic windows forms application.
I have a class called class1 and another called game. It is my understanding that the autogenerated form1.h is also a class. Maybe this is because i don't understand.

Anyway from game i can declare a pointer of type class1 and access class1's data members, functions etc from a function inside game. I can also declare a pointer of type form1. However when i come to access form1's data members using this pointer it won't let me alter them it just says:

error C2227: left of '->richtextbox1' must point to class/struct/union/generic typ

Here is the code:

//game.h

#pragma once
class class1; //I was told these lines make this class aware of the
class form1; //other class it needs to access.
class game

{
public:
int room;
form1* form1;
class1* class1;
game(void);
public:
int travel(game game);
virtual ~game(void);
};


//game.cpp
#include "StdAfx.h"
#include "game.h"


game::game(void)
{

}

game::~game(void)
{
}

game::travel(game game)
{
class1->datamember =2; //this line works fine
form1->richtextbox1 ="hello world"; //this dies horribly

return 0;
}

I would be extremely grateful to anyone who could help me. Thanks


Re: Visual C++ Language Windows forms help

Marius Bancila

Lutzee wrote:
Anyway from game i can declare a pointer of type class1 and access class1's data members, functions etc from a function inside game. I can also declare a pointer of type form1. However when i come to access form1's data members using this pointer it won't let me alter them it just says:

error C2227: left of '->richtextbox1' must point to class/struct/union/generic typ

That is because the type form1 is not known.


#pragma once
class class1; //I was told these lines make this class aware of the
class form1; //other class it needs to access.

That is called forward declaration. It's not enough. In the cpp file you'll still have to include the header, which you don't do.


class game

{
public:
int room;
form1* form1;
class1* class1;
game(void);
public:
int travel(game game);
virtual ~game(void);
};

Duh... don't use type names as variables. form1 is a type, you use it as a variable name too. Put an underscore or something.

form1* form1_;

class1* class1_;


//game.cpp
#include "StdAfx.h"
#include "game.h"

You did not include neither "form1.h" nor "class1.h".

I suggest you get a good C++ book and learn the language.

Here is a list of C++ FAQs that may also help you.






Re: Visual C++ Language Windows forms help

Lutzee

i used those variables names as examples so that the problem would be clear. In my project they are called p_class1 and p_game





Re: Visual C++ Language Windows forms help

Marius Bancila

Did you fix the problem




Re: Visual C++ Language Windows forms help

Lutzee

It is still eluding me. i tried what you said to do. i even tried itin a new project to check it wasn't somthing i had inadvertantly changed that was causeing the problem. Here is the new project: There are 2 classes c1 and c2.

//Form1.h

#pragma once

 

namespace example {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

/// <summary>

/// Summary for Form1

///

/// WARNING: If you change the name of this class, you will need to change the

/// 'Resource File Name' property for the managed resource compiler tool

/// associated with all .resx files this class depends on. Otherwise,

/// the designers will not be able to interact properly with localized

/// resources associated with this form.

/// </summary>

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::Label^ label1;

private: System::Windows::Forms::Button^ button1;

protected:

private:

/// <summary>

/// Required designer variable.

/// </summary>

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

void InitializeComponent(void)

{

this->label1 = (gcnew System::Windows::Forms::Label());

this->button1 = (gcnew System::Windows::Forms::Button());

this->SuspendLayout();

//

// label1

//

this->label1->AutoSize = true;

this->label1->Location = System::Drawing::Point(61, 39);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(35, 13);

this->label1->TabIndex = 0;

this->label1->Text = L"label1";

//

// button1

//

this->button1->Location = System::Drawing::Point(129, 37);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(80, 48);

this->button1->TabIndex = 1;

this->button1->Text = L"button1";

this->button1->UseVisualStyleBackColor = true;

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(284, 264);

this->Controls->Add(this->button1);

this->Controls->Add(this->label1);

this->Name = L"Form1";

this->Text = L"Form1";

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

};

}

///c1.cpp

#include "StdAfx.h"

#include "Form1.h"

#include "c2.h"

#include "c1.h"

c1::c1(void)

{

}

c1::function(void)

{

c2_->example=2; ///why does this work

f1->label1->Text="hello"; //whereas this does not As far as i can see the code is exactly the same.

}

c1::~c1(void)

{

}

///c1.h

#pragma once

class c2;

class Form1;

class c1

{

public:

c1(void);

Form1* f1;

c2* c2_;

int function(void);

public:

~c1(void);

};

///c2.cpp

#include "StdAfx.h"

#include "c2.h"

c2::c2(void)

{

}

c2::~c2(void)

{

}

///c2.h

#pragma once

class c2

{

public:

c2(void);

int example;

public:

~c2(void);

};

 

Here are the errors generated:

1>------ Build started: Project: example, Configuration: Debug Win32 ------

1>Compiling...

1>c1.cpp

1>.\c1.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>.\c1.cpp(13) : error C2027: use of undefined type 'Form1'

1> c:\users\lutzee\documents\visual studio 2005\projects\example\example\c1.h(3) : see declaration of 'Form1'

1>.\c1.cpp(13) : error C2227: left of '->label1' must point to class/struct/union/generic type

1>.\c1.cpp(13) : error C2227: left of '->Text' must point to class/struct/union/generic type

1>Build log was saved at "file://c:\Users\Lutzee\Documents\Visual Studio 2005\Projects\example\example\Debug\BuildLog.htm"

1>example - 4 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

 

Thanks very much for you r help :)