PDA

View Full Version : 'Class' does not name a type error



naturalpsychic
31st January 2011, 17:43
hi, its probably some silly mistake in c++ but my code is not working, it gives me error.

here is code

_time.h


#ifndef _TIME_H
#define _TIME_H

#include <QObject>
#include <QTimer>

class _Time : public QObject
{
Q_OBJECT
public:
_Time();
_Time(int sec=0,int min=0,int hour=0);
int hour;
int min;
int sec;
};
#endif // _TIME_H


_time.cpp


#include "_time.h"

_Time::_Time()
{

hour=0;
min=0;
sec=0;

}
_Time::_Time(int s, int m, int h)
{

if (h > -1)
hour=h;
else
hour=0;

if ((m > -1) && (m<60))
min=m;
else
min=0;

if ((s > -1) && (s<60))
sec=s;
else
sec=0;


}


main.cpp



#include "_time.h"
_Time t;
int main(int argc, char *argv[])
{


return 0;
}



i get error:
_Time does not name a type.

any suggestions? thanks in advance

Lykurg
31st January 2011, 17:53
You are missing a semi colon at your class declaration.

naturalpsychic
31st January 2011, 18:04
thanks for reply but im sorry in actual code there is semi colon (forgot to put in forum)..
even with semi colon it does not work

stampede
1st February 2011, 00:32
naturalpsychic, I have a riddle for you.

Let us consider following code:


_Time t;

Which one of those two constructors

1) _Time();
2) _Time(int sec=0,int min=0,int hour=0);
should be called ?

Do you know the answer ? :rolleyes:

ChrisW67
1st February 2011, 03:28
This exactly what my compiler tells me too.


main.cpp:2: error: call of overloaded ‘_Time()’ is ambiguous
_time.h:12: note: candidates are: _Time::_Time(int, int, int)
_time.h:11: note: _Time::_Time()
main.cpp:3: warning: unused parameter ‘argc’
main.cpp:3: warning: unused parameter ‘argv’
make: *** [main.o] Error 1


You do not need your no-arguments constructor.

The difference in error messages aside, from the 2003 C++ Standard:


17.4.3.2.1 Global names [lib.global.names]

Certain sets of names and function signatures are always reserved to the implementation:

* Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
* Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace. [165]

[165] Such names are also reserved in namespace ::std (17.4.3.1).

So _Time is a bad name.

naturalpsychic
1st February 2011, 15:58
thanks for reply

what i have done now is i changed my '_Time' class name to 'CMTime', thanks for the reference,

also now i have gotten rid of one of constructor now i have got one constructor,

i still get this error (CMTime does not name a type)

and if i get rid of
#ifndef _TIME_H
#define _TIME_H
...
#end _TIME_H
macro definition i get error for multi declaration of class, i had made sure myself by re-writing class with same functions, but its still doing that

thanks in advance

Lykurg
1st February 2011, 16:19
please send a minimal compilable example reproducing the error.

naturalpsychic
1st February 2011, 16:25
_time.h


#ifndef _TIME_H
#define _TIME_H

#include <QObject>
#include <QTimer>

class CMTime : public QObject
{
Q_OBJECT
public:
//CMTime();
CMTime(int sec=0,int min=0,int hour=0);
void startTicking();
QString stop();

QString restart();
int getHour();
int getMin();
int getSec();
int getTotalSeconds();
static QString getTime(int h,int m, int s);
private:
QTimer *timer;
int hour;int min;int sec;
signals:
void secondPast(int h,int m,int s);
public slots:
void timeout();

};

#endif // _TIME_H

_time.cpp


#include "_time.h"

/*CMTime::CMTime()
{
timer=new QTimer(this);
hour=0;
min=0;
sec=0;
QObject::connect(timer,SIGNAL(timeout()),this,SLOT (timeout()));

}*/
CMTime::CMTime(int s, int m, int h)
{
timer=new QTimer(this);
if (h > -1)
hour=h;
else
hour=0;

if ((m > -1) && (m<60))
min=m;
else
min=0;

if ((s > -1) && (s<60))
sec=s;
else
sec=0;

QObject::connect(timer,SIGNAL(timeout()),this,SLOT (timeout()));
}
int CMTime::getHour()
{
return hour;
}
int CMTime::getMin()
{
return min;
}
int CMTime::getSec()
{
return sec;
}
void CMTime::timeout()
{
emit secondPast(hour,min,sec);
if (sec++ == 59)
{
sec=0;
if (min++ == 59)
{
min=0;
hour++;
}
}


}

QString CMTime::restart()
{
QString result=getTime(getHour(),getMin(),getSec());
sec=0;
hour=0;
min=0;
startTicking();
return result;
}
int CMTime::getTotalSeconds()
{
return (sec + (min * 60) + ((hour * 60) * 60));
}
QString CMTime::getTime(int h,int m,int s)
{
QString resultTime;
QString sStr;QString mStr;QString hStr;
s < 10 ? sStr="0"+QString::number(s):sStr=QString::number(s);
m < 10 ? mStr="0"+QString::number(m):mStr=QString::number(m);
h < 10 ? hStr="0"+QString::number(h):hStr=QString::number(h);

resultTime= hStr+ ":"+mStr+":"+sStr;
return resultTime;
}
void CMTime::startTicking()
{
timer->start(1000);
}
QString CMTime::stop()
{
timer->stop();
return getTime(getHour(),getMin(),getSec());
}



_process.h:


#ifndef PROCESS_H
#define PROCESS_H

#include <QThread>

#include "_time.h"
#include <QTreeWidget>
class CMProcess : public QThread
{
Q_OBJECT
public:
explicit CMProcess(QTreeWidget *parent,QWidget *parentWid);


CMTime t;//<< here it gives error


};

#endif // PROCESS_H

arnaiz
1st February 2011, 16:42
natural...
I think you have an error, because I "make" your source code with the changes suggested, and it worked. Maybe you omit making one of the "CM" changes in the class declaration...?
Hope this helps.

To late... :-(

naturalpsychic
1st February 2011, 16:43
oh i solved it myself..just in case you guys wondering i changed header macro name to CMTIME_H

thanks anyways guys