Cannot access an object into a class

Hello,

I have a problem to use the function writeErrorMsg in errorMsg.h because of the use of the parameter MainForm in it. I don't why I can't use it like that..

errorMsg.h:

1
2
3
4
5
6
7
8
  #include "mainform.h"
class errorMsg : public QObject
{
public:
  errorMsg();
  ~errorMsg();
  void writeErrorMsg(QString msg, QString type_msg, MainForm &Window);
};


errorMsg.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
#include "errorMsg.h"

void errorMsg::writeErrorMsg(QString msg, QString type_msg, MainForm &Window)
{
	QColor colorLine;
	
	if (type_msg=="type_error")
	{
	   colorLine.setRgb(214,30,16);
	}
	Window.insertTextInErrorBrowser(msg, colorLine);
}


mainform.h:

1
2
3
4
5
6
7
8
9
10
#include "errorMsg.h"

class MainForm : public QMainWindow
{
Q_OBJECT
  public:
void insertTextInErrorBrowser(QString msg, QColor colorLine);
  private:
Ui::MainForm *ui;
};


mainform.cpp:

1
2
3
4
5
6
7
8
9
10
11
#include "mainform.h"

void MainForm::insertTextInErrorBrowser(QString msg, QColor colorLine)
{
	ui->teErrorBrowser->moveCursor(QTextCursor::End);
	ui->teErrorBrowser->setTextColor(colorLine);
	ui->teErrorBrowser->setFontWeight(QFont::Bold);
	ui->teErrorBrowser->insertPlainText(msg + "\n");
	colorLine.setRgb(0,0,0);
	ui->teErrorBrowser->setTextColor(colorLine);
}


The error message is : error C2061: syntax error : identifier 'MainForm'. Would anyone have an idea about what is wrong about that and how to fix it? Thank you in advance!

Last edited on
You have circular includes. mainform.h includes errormsg.h while errormsg.h includes mainform.h. See this link:
http://stackoverflow.com/questions/17865286/c-circular-include

Your MainForm class does not appear to rely on errorMsg class, so I would suggest removing line 1 from mainform.h.

Your problem lies in the that you #include "mainform.h" in errorMsg.h and #include "errorMsg.h" in mainform.h which causes it to go into recursive inclusion.

What is happening here is that when the compiler starts to scan errorMsg.h it sees that it must include the header mainform.h (#include "mainform.h" ), so it heads on over to that file and starts "scanning" mainform.h the first thing it hits in that file is the include statement for errorMsg.h so it heads back over to errorMsg.h which then see the include statement in that file for mainform.h so it heads back over to there... This goes on and on, the compiler will eventually give up trying to compile and throw a error.

The way you can combat this is to use forward declarations of the class in the header file and then do the #include statement in the .cpp file for that class instead.

Though yours can be fixed without having to use forward declaration. Just delete the #include "errorMsg.h" statement from the mainform header and add #include "errorMsg.h" to the mainform.cpp instead.

This will get rid of that error. Only put includes in the header file for the ones you absolutely need in the header file, if you can get by without it in the header file and only in the .cpp file declare it in the .cpp file.
Thank you for your reply. I think I get it.

I removed it but now a new error appears:
error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'
This sounds to me like you are trying to copy a QMainWindow somewhere. This could be something like using a = operator on a QMainWindow or trying to use copy construction. Since you MainForm inherits from QMainWindow this sounds like a good place to start looking.

The reason why this would give a error is because QMainWindow's '=' operator and copy constructor are private as to not allow copying of windows.

But sadly I am just guessing at this because there isn't enough information provided. Could provide the exactly line and file that the error code is saying the error is. Also could you provide the complete source code for said file in question.
Topic archived. No new replies allowed.