this post was submitted on 04 Apr 2026
5 points (100.0% liked)

C++

2259 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
 

So i am basically almost done with my C++ App Logic Wise tho the only thing i struggle with and wanna figure it is how to include a seperate class into my main class constructors >.>

Mostly due to the fact that currently in my Code my main Code has 2 Objects tho ErrorWin Object is right now the only one that exist twice as seperate Objects which itd like to fix >.>

So this is my first Object in my Main Function which just calls my DisplayWindow Function while my ErrorWin Object calls the Display Error Window Function :P

int main() {
  ErrorWindow ErrorWin;
  MainWindow MainWin;

  if (ProgramPath.empty()) {
    ErrorWin.DisplayErrorWindow();
    return Fl::run();
  }

  MainWin.DisplayMainWindow();
  return Fl::run();
}

Now the Main Issue is that only my First Text basically gets displayed in the Error Window even tho my switch Statement is set to display a different error text basically according to my callback but that obviously does not work due to theyre being seperate ErrorWin Objects :(

void MainWindow::CallbackSaveFile(Fl_Widget* Widget, void* Data) {
  MainWindow* MainWindowData = static_cast<MainWindow*>(Data);
  ErrorWindow ErrorWin;

  if (!MainWindowData->InputFile.empty()) {
    ...
  } else {
    ErrorWin.DisplayErrorWindow();
  }
}
you are viewing a single comment's thread
view the rest of the comments
[–] Oka@sopuli.xyz 2 points 2 days ago (1 children)

Initialize ErrorWin as a pointer. Instantiate it in main (or at initialization).

Remember to delete the data in the destructor.

[–] RetroHax@feddit.org 1 points 2 days ago (1 children)

huh? O.o
so you mean like this for Example? >.>

int main() {
  ErrorWindow* ErrorWin;
  MainWindow MainWin(&ErrorWin);

and then inside mainwindow source files like this? O.o

class MainWindow {
  public:
    MainWindow(ErrorWindow* ErrorWin);
  private:
    ErrorWindow* ErrWin;

MainWindow::MainWindow(ErrorWindow* ErrorWin) : ErrWin(ErrorWin) {

because all i recieve is this error even tho i included errorwindow.hpp inside mainwindow.hpp and cpp file >.>

In file included from src/gui/errorwindow.hpp:20,
                 from src/main.cpp:13:
src/gui/mainwindow.hpp:56:27: error: expected ‘)’ before ‘*’ token
   56 |     MainWindow(ErrorWindow* ErrorWin);
      |               ~           ^
      |                           )
src/gui/mainwindow.hpp:83:5: error: ‘ErrorWindow’ does not name a type
   83 |     ErrorWindow* ErrWin;
      |     ^~~~~~~~~~~
make: *** [Linux.mk:39: build/main.o] Error 1
[–] p_consti@lemmy.world 2 points 2 days ago (1 children)

First, in your main, you want judt ErrorWindow ErrorWin;. As it is, you're taking a pointer to a pointer with the & (also it's not initialized).

Second, does your MainWindow.hpp know the ErrorWindow? You may have to forward declare it (write class ErrorWindow; at the top, to tell the compiler it exists without saying anything else). If you include the other header files respectively, they include each other, so you get a cycle.

[–] RetroHax@feddit.org 1 points 2 days ago (1 children)

yup just removed the infinite cycle by just doing class ErrorWindow; inside mainwindow.hpp :P
also if i remove MainWindow MainWin; from main then obviously all i get is a compiler Error as i require MainWin inside my Main Function >.>

src/main.cpp:20:5: error: ‘MainWin’ was not declared in this scope; did you mean ‘MainWindow’?
[–] p_consti@lemmy.world 1 points 2 days ago

I did not mean to remove MainWindow from main, I meant you shoud change ErrorWindow* ErrorWin to ErrorWindow ErrorWin, because you don't want a pointer variable, you want an instance, and then you pass a pointer to the instance using &ErrorWin as you did.