PDA

View Full Version : Does not define type



Atomic_Sheep
18th July 2013, 12:27
I have one class:

field.h


#ifndef FIELD_H
#define FIELD_H

class Field : public QWidget
etc

#endif


And then I have another class that uses this class as composition:

Page.h


#ifndef PAGE_H
#define PAGE_H

#include "field.h"

class Page : public QWidget
{
Field array[10];
}

#endif


At compile I get the error of 'Field' does not name a type, I don't know how to fix this? Internet seems to spring up a lot of circular includes as being the culprit however I don't understand how this is the case given that this is composition and I've got all my includes all up and running and it seems to work here:

http://www.learncpp.com/cpp-tutorial/102-composition/

!!!!!!!!!!!!!!!!!!!!!!!!! << sorry coding a line a week is frustrating. Every time something is added, a new error comes about and it's just beyond frustrating. Programing isn't my forte I don't think.

Atomic_Sheep
19th July 2013, 08:53
field.h

#ifndef FIELD_H
#define FIELD_H

#include <QWidget>
#include <QMessageBox>
#include <QDebug>
#include <QString>
#include <iostream>
#include <string>

class Field : public QWidget
{
Q_OBJECT
public:
explicit Field(QWidget *parent = 0);

int m_iRow;
int m_iColumn;
std::string m_sText;
int m_iColour;

void SetColour(int iColour);
char FieldText();
};

#endif


page.h

#ifndef PAGE_H
#define PAGE_H

#include <QWidget>

#include <iostream>
#include <string>

#include "field.h"
#include "selectablefield.h"
#include "editablefield.h"

class Page : public QWidget
{
Q_OBJECT
public:
explicit Page(QWidget *parent = 0);

Page()
{}

Field m_anField[220];

signals:

public slots:

};

#endif

Error that I get is:

\page.h:39: error: 'Field' does not name a type

compile output is:

In file included from ../mainwindow.h:6,
/field.h:13,
from \field.cpp:1:
/page.h:39: error: 'Field' does not name a type
mingw32-make[1]: Leaving directory `D:/Users/...
mingw32-make[1]: *** [debug/field.o] Error 1
mingw32-make: *** [debug] Error 2
The process "D:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project ... (target: Desktop)
When executing build step 'Make'

ChrisW67
22nd July 2013, 07:01
What does the top of field.cpp look like? Is it really in the root directory of you drive as this would imply?

In file included from ../mainwindow.h:6,
/field.h:13,
from \field.cpp:1: <<<<< here


Is everything in the same directory?
Is there more than one file called "field.h" in your project?

Atomic_Sheep
22nd July 2013, 14:55
Found the error, you were pretty close from what I can gather. I had an #include field.h in my main.cpp file and for some reason that was what was causing the issue. I'm not sure why this was the case given that I was using header guards.

wysota
22nd July 2013, 15:04
Your code is invalid -- widgets cannot be copied thus you shouldn't have an array of Field objects. Instead you can have an array of Field pointers.