私はc++でコードを書きました。KdevelopでCMakeを使ってみました。
私のヘッダーファイル Newtime.h
class NewTime
{
public:
NewTime(int = 0, int = 0, int = 0); //constructor
void setTime(int, int, int); // set time
void dispTime(); //print time
private:
int hour;
int minute;
int second;
};
NewTime.cpp については、以下に書きました。
#include <iostream>
#include <stdio.h>
#include "NewTime.h"
//
NewTime::NewTime(int hr, int min, int sec)
{
setTime(hr, min, sec);
}
//
void NewTime::setTime(int h, int m, int s)
{
hour = ((h >= 0 && h < 24) ? h : 0);
minute = ((m >= 0 && m < 60) ? m : 0);
second = ((s >= 0 && s < 60) ? s : 0);
}
//***
void Newtime::dispTime()
{
std::cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)
<< " : " << (minute < 10 ? "0" : "") << minute
<< " : " << (second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM") << std::endl;
}
本体の場合は次のとおりです。
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include "NewTime.h"
int main()
{
NewTime t1, //use default arguments
t2(2), //only hour defined
t3(21, 34), //second as default
t4(12, 25, 42), //everything is defined
t5(53, 343, 234); //invalid hour
std::cout << "constructed with: " << std::endl;
std::cout << "use default arguments: " << std::endl;
t1.dispTime();
std::cout << "only hour: " << std::endl;
t2.dispTime();
std::cout << "hour and minute: " << std::endl;
t3.dispTime();
std::cout << "all defined: " << std::endl;
t4.dispTime();
std::cout << "invalid values: " << std::endl;
t5.dispTime();
std::cin.get();
return 0;
}
しかし、私はこのエラーが発生しました:
compiling NewTime.cpp (c++)
/home/hasadi/Desktop/tmp/C++/4-6-class-newtimer/newtimer/NewTime.cpp:17: error: ‘Newtime’ has not been declared
/home/hasadi/Desktop/tmp/C++/4-6-class-newtimer/newtimer/NewTime.cpp: In function ‘void dispTime()’:
/home/hasadi/Desktop/tmp/C++/4-6-class-newtimer/newtimer/NewTime.cpp:19: error: ‘hour’ was not declared in this scope
/home/hasadi/Desktop/tmp/C++/4-6-class-newtimer/newtimer/NewTime.cpp:20: error: ‘minute’ was not declared in this scope
/home/hasadi/Desktop/tmp/C++/4-6-class-newtimer/newtimer/NewTime.cpp:21: error: ‘second’ was not declared in this scope
何か案が?私は混乱しました..
事前にご協力いただきありがとうございます