Results 1 to 2 of 2

Thread: Error message when try to instance an object.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Error message when try to instance an object.

    Hi!

    So, I have this class inside a namespace but when i try to create an instance i get the following error message:
    Qt Code:
    1. #ifndef FILE_H
    2. #define FILE_H
    3.  
    4. #include "constants.h"
    5.  
    6. #include <cstdlib>
    7.  
    8. #include <QVector>
    9.  
    10. namespace Compressor {
    11. class File;
    12. }
    13.  
    14. class File {
    15. public:
    16. QVector<QVector<int>> bytes;
    17. int length;
    18. public:
    19. File(int l);
    20. };
    21.  
    22. #endif // FILE_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "file.h"
    2.  
    3. File::File(int l) {
    4. length = l;
    5.  
    6. for(int i = 0; i < length; i++) {
    7. QVector<int> b;
    8.  
    9. for(int j = 0; j < BYTESIZE; j++)
    10. b.push_back(rand( ) % 2);
    11.  
    12. bytes.push_back( b );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "file.h"
    5. #include "tableconfig.h"
    6. #include "tableshow.h"
    7.  
    8. #include <QMainWindow>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow {
    15. Q_OBJECT
    16. public:
    17. Compressor::File *fl;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::on_buttonOrigin_clicked( ) {
    2. fl = new Compressor::File( ui->spinBox->value( ) ); // The problem happens here.
    3. showBytesTable(ui->tableOrigin, fl);
    4. }
    To copy to clipboard, switch view to plain text mode 
    /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?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Error message when try to instance an object.

    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++.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QApplication Instance Error
    By Nfrancisj in forum Newbie
    Replies: 4
    Last Post: 30th April 2016, 06:36
  2. Replies: 2
    Last Post: 18th April 2015, 17:48
  3. Replies: 1
    Last Post: 7th February 2012, 23:04
  4. Multiple instance of Plugin object
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2011, 17:11
  5. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2009, 00:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.