我是C ++的新手。我们目前正在学习面向对象的编程,但我的程序出错了。我通常使用Visual Studios和编译好的程序。当我在Mimir上提交它时,这是一个自动检查你的工作的程序,它声明它不想编译。我继续尝试在CodeBlocks上运行它并且它不能编译。我真的很难过这个问题。我99%确定它是我的代码中的错误,而不是编译器。
print(x, '\t', 2**, sep='' )
我收到错误: 编译器/调试输出:
*ClockMan.cpp*
#include "CClockType.h"
#include <iostream>
using namespace std;
int main(){
CClockType myclock;
myclock.printTime();
myclock.setTime(10,9,35);
myclock.printTime();
CClockType yourclock;
yourclock.setTime(10, 9, 36);
yourclock.printTime();
CClockType Figueroaclock (12, 0, 0);
Figueroaclock.printTime();
CClockType classClocks[10];
for (int i = 0; i < 10; i++)
classClocks[i].setTime(9,27,30);
CClockType * ptrClock = & (classClocks[1]);
ptrClock = &yourclock;
ptrClock ->setTime(9, 32, 20);
ptrClock = classClocks;
for (int i = 0; i < 10; i++){
ptrClock->setTime(9,27,30);
ptrClock++;
}
int size = 2;
ptrClock = new CClockType[size];
CClockType * tmpClock = ptrClock;
for (int i = 0; i < size; i++){
tmpClock->setTime(9,27,30);
tmpClock++;
}
delete [] ptrClock;
cin.ignore(1124, '\n');
cin.get();
return 0;
}
*CClockType.h*
// CClockType header file : class declaration
class CClockType{
private:
int hr, mins, sec;
public :
//constructor : declare a way to initialize our member variables
// how to write a constructor (special member function,
// where the function name is the class name and which syntax is similar to member function but
// the constructor need no return specifier
// we can declare diverse constructors
CClockType();
CClockType(int hours, int minutes, int seconds);
//destructor : special member function which can be used to define how to release all members variables
//only one destructor is allowed without parameters and with ~ leading the function name
~CClockType();
void setTime(int , int , int ); //member function prototype
void printTime();
void getTime(int *hours, int *minutes, int *seconds);
void incrementTimeBySeconds();
void incrementTimeByMinutes();
void incrementTimeByHours();
};
*CClockTypeImpl.cpp*
#include "CClockType.h"
#include <iostream>
using namespace std;
/*
void setTime(int hours, int minutes, int seconds);
void setTime2(int hours, int minutes, int seconds);
void getTime(int *hours, int *minutes, int *seconds);
void incrementTimeBySeconds();
void incrementTimeByMinutes();
void incrementTimeByHours(); */
// TODO Define a constructor with no parameter list
CClockType::CClockType() {
hr = 0;
mins = 0;
sec = 0;
}
// TODO Define a constructor with three int parameters for setting up hours, minutes, and seconds
CClockType::CClockType(int hours, int minutes, int seconds) {
setTime(hr, mins, sec);
}
// Define a destructor
CClockType::~CClockType() {
setTime(hr, mins, sec);
}
// Define a member function
// (Here we specify the class which function belongs to so that the member function can use all variables and other functions declared in the class )
// static member function is an exception
// TODO Define the member function named setTime, which need three int parameters, hours, minutes, and seconds and return nothing
void CClockType::setTime(int hours, int minutes, int seconds) {
if (0 <= hours && hours < 24) // hours can not be less than 0 nor bigger than 24
hr = hours; // initialize
else
hr = 0;
if (0 <= minutes && minutes < 60) // minutes can not be less than 0 nor bigger than 60
mins = minutes; // initialize
else
mins = 0;
if (0 <= seconds && seconds < 60) // seconds can not be less than 0 nor bigger than 60
sec = seconds; // initialize
else
sec = 0;
}
/*
TODO Define member function CClockType::getTime
Goal: read out the hours, minutes, and seconds and save them to the outside variables
Inputs: pointers referring to outside variables used to save hours, minutes, and seconds
Output: void
*/
void CClockType::getTime(int *hours, int *minutes, int *seconds) {
*hours = hr;
*minutes = mins;
*seconds = sec;
}
/*
TODO Define member function CClockType::incrementTimeBySeconds
Goal: to adjust time by increasing seconds by 1.
Inputs:
Output: void
*/
void CClockType::incrementTimeBySeconds() {
sec++;
if (sec > 59) {
sec = 0;
incrementTimeByMinutes(); // Increment Minute
}
}
/*
TODO Define member function CClockType::incrementTimeByMinutes
Goal: to adjust time by increasing seconds by 1.
Inputs:
Output: void
*/
void CClockType::incrementTimeByMinutes() {
mins++;
if (mins > 59) {
mins = 0;
incrementTimeByHours(); // Increment Hour
}
}
/*
TODO Define member function CClockType::incrementTimeByHours
Goal: to adjust time by increasing seconds by 1.
Inputs:
Output: void
*/
void CClockType::incrementTimeByHours() {
hr++;
if (hr > 23)
hr = 0;
}
/*
TODO Define member function CClockType::printTime
Goal: to adjust time by increasing seconds by 1.
Inputs:
Output: void
*/
void CClockType::printTime() {
setTime(hr, mins, sec);
cout << hr << ":";
cout << mins << ":";
cout << sec << endl;
}
测试用例的正确输出
g++: error: *.cpp: No such file or directory
g++: error: *.h: No such file or directory
g++: fatal error: no input files
compilation terminated.
:g++: error: *.cpp: No such file or directory
g++: error: *.h: No such file or directory
g++: fatal error: no input files
compilation terminated.
就像我提到的,当我在Visual Studios中运行它时,我得到了正确的输出,但是当我提交它时,它声明它不能编译。它也不在Codeblocks中编译。任何帮助表示赞赏!提前谢谢!