-2

So, I have this class inside a namespace but when i try to create an instance i get the following error message:

#ifndef FILE_H
#define FILE_H
#include "constants.h"
#include <cstdlib>
#include <QVector>

namespace Compressor {
class File;
}

class File {
public:
    QVector<QVector<int>> bytes;
    int length;
public:
    File(int l);
};

#endif // FILE_H

...

#include "file.h"

File::File(int l) {
    length = l;

    for(int i = 0; i < length; i++) {
        QVector<int> b;

        for(int j = 0; j < BYTESIZE; j++)
            b.push_back(rand( ) % 2);

        bytes.push_back( b );
    }
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

...

#include "file.h"
#include "tableconfig.h"
#include "tableshow.h"

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    Compressor::File *fl;
//...
//...

...

void MainWindow::on_buttonOrigin_clicked( ) {
    fl = new Compressor::File( ui->spinBox->value( ) ); // The problem happens here.
    showBytesTable(ui->tableOrigin, fl);
}

Here the error message:

/home/roger/Programming/C-C++/Linux/Qt/Compressor-4bits/mainwindow.cpp:13: error: invalid use of incomplete type ‘class Compressor::File’ fl = new Compressor::File( ui->spinBox->value( ) ); ^

How can I fix that?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Roger_88
  • 477
  • 8
  • 19
  • 3
    `Compressor::File` is in no way related to `File`. And since `Compressor::File` is not defined (it is only declared), it is of an incomplete type. What, exactly, is unclear? – Algirdas Preidžius Jan 04 '18 at 22:45
  • @AlgirdasPreidžius Well, it might be not so evident for someone with less experience. Maybe your comment could be the answer for this question. – Oscar Jan 04 '18 at 22:47
  • 3
    @Oscar In that case, I would advise that someone to read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), since topic of namespaces should be covered in those. – Algirdas Preidžius Jan 04 '18 at 22:49
  • I tried to do the same thing as "mainwindow.h" created automatically when i use qtcreator. In this class definition i have "namespace Ui { class MainWindow; } class MainWindow : public QMainWindow {//..." – Roger_88 Jan 04 '18 at 23:43

1 Answers1

2

You are closing the namespace right behind the first line:

namespace Compressor {
class File;
}

and then your complete class File is defined outside of the namespace. This is another File for the compiler than the one you start inside the namespace (which contains nothing, just a forward declaration)

Aganju
  • 6,295
  • 1
  • 12
  • 23