Reputation: 25
I'm a beginner and I want to use instance w
of OlMainWindow
in order to access _x
by pressing address (OlMain->vref->_x=10;
) through ui_MainWidow::setupUI()
function, but when I compile this program then I get an error on line OlMain->vref->_x=10
:
error: use of undefined type 'OlMainWindow'
Please help me fix this error.
ui_MainWidow.h
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <iostream>
#include "olvalue.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class ui_MainWidow {
public:
void setupUI(OlMainWindow* OlMain) {
OlMain->vref->_x=10; // _x = 10;
OlMain->vref->_x = 10;
std::cout << " ok";
}
};
#endif //UI_MAINWINDOW_H
olvalue.h
#include <iostream>
#ifndef VALUE_H
#define VALUE_H
#include "ui_mainwindow.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class OlValue
{
public:
int _x;
int _y;
public:
OlValue() {
_x = 10;
_y = 20;
}
};
#endif //VALUE_H
olmainwindow.h
#ifndef OLMAINWINDOW_H
#define OLMAINWINDOW_H
#include <iostream>
#include <string>
#include "olvalue.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class OlMainWindow
{
public:
OlValue* vref = new OlValue;
OlMainWindow();
~OlMainWindow();
void enterUsername();
private:
int val;
std::string _Username;
};
#endif //OLMAINWINDOW_H
olmainwidow.cpp
#include "olmainwindow.h"
#include <iostream>
#include <string>
class OlValue;
class OlMainWindow;
class ui_MainWidow;
OlMainWindow::OlMainWindow() {
ui_MainWidow* ui = new ui_MainWidow();
ui->setupUI(this);
}
OlMainWindow::~OlMainWindow() {
//delete ui;
}
void OlMainWindow::enterUsername() {
std::cout << "Please, enter Username: ";
std::getline(std::cin, _Username);
std::cout << "You'v accessed Username: " << _Username << std::endl;
}
main.cpp
#include "olmainwindow.h"
#include <iostream>
int main(int argc, char* argv[])
{
OlMainWindow* w = new OlMainWindow();
return 0;
}
Upvotes: 0
Views: 52
Reputation: 597101
In ui_MainWidow.h
, the implementation of setupUI()
is inlined, but when it tries to access the members of OlMainWindow
it fails because the OlMainWindow
class hasn't actually been defined yet, merely forward declared.
Your files are not setup correctly. You are creating dependencies where they don't belong, and then you are trying to use forward declarations to solve circular dependencies issues that shouldn't exist in the first place.
Try this instead:
OlValue.h
#ifndef OLVALUE_H
#define OLVALUE_H
class OlValue
{
public:
int _x = 10;
int _y = 20;
};
#endif //OLVALUE_H
ui_MainWindow.h
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
class OlMainWindow;
class ui_MainWindow {
public:
void setupUI(OlMainWindow* OlMain);
};
#endif //UI_MAINWINDOW_H
ui_MainWindow.cpp
#include "ui_MainWindow.h"
#include "OlMainWindow.h"
#include <iostream>
void ui_MainWindow::setupUI(OlMainWindow* OlMain) {
OlMain->vref->_x = 10;
OlMain->vref->_x = 10;
std::cout << " ok";
}
olMainWindow.h
#ifndef OLMAINWINDOW_H
#define OLMAINWINDOW_H
#include "OlValue.h"
#include <string>
class ui_MainWidow;
class OlMainWindow
{
public:
OlValue* vref = new OlValue;
OlMainWindow();
~OlMainWindow();
void enterUsername();
private:
ui_MainWidow* ui;
int val;
std::string _Username;
};
#endif //OLMAINWINDOW_H
olMainWindow.cpp
#include "olMainWindow.h"
#include <iostream>
#include <string>
OlMainWindow::OlMainWindow() {
ui = new ui_MainWindow();
ui->setupUI(this);
}
OlMainWindow::~OlMainWindow() {
delete ui;
}
void OlMainWindow::enterUsername() {
std::cout << "Please, enter Username: ";
std::getline(std::cin, _Username);
std::cout << "You'v accessed Username: " << _Username << std::endl;
}
main.cpp
#include "olMainWindow.h"
#include <iostream>
int main(int argc, char* argv[])
{
OlMainWindow w;
w.enterUsername();
return 0;
}
Upvotes: 1