PDA

View Full Version : Error message when try to instance an object.



robgeek
4th January 2018, 21:32
Hi!

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);
}

/home/rob/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?

d_stranz
5th January 2018, 22:15
How can I fix that?

You have forward declared that there is a class named "File" in the "Compressor" namespace in lines 10 - 12 of your header file. You then go on to declare a class named "File" in the global namespace (lines 14 and onward) because you have not enclosed the class declaration in a namespace {} qualifier. Likewise, your implementation of the File class in file.cpp is also in the global namespace because you have not qualified the File:: methods with Compressor:: File:: or enclosed the entire set of method definitions within a namespace Compressor {} declaration.

The compiler is telling you that it's aware of a class called File in the Compressor namespace, but it can't find any code that implements it.

Pretty basic C++.