包含iostream时实例化自定义类时间时出错

时间:2018-02-11 09:56:58

标签: c++ oop

以下是代码:

const postSchema = new Schema({
    id: { type: Number, required: true, unique: true },
    slug: { type: String, required: true, unique: true },
    creatorId: { type: Number, required: true },
    title: { type: String, required: true },
    image: { type: String, required: false },
    description: { type: String, required: true },
    content: { type: Array, required: false },
    createdAt: { type: Date, default: Date.now },
})

编译器错误:

//main.cpp
#include <iostream>
#include "time.h"

int main(int argc, char** argv) {
    time::time t;
    return 0;
}

//time.cpp
#include "time.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;

time::time(){
    ore = minuti = secondi = 0;
} //costruttore senza argomenti

void time::setTime(int h,int m, int s){
    if(!(h<=0 && h>24)&& !(m<0 && m>60)&&!(s<=0 && s>60)){
        ore = h;
        minuti = m;
        secondi = s;
    }else{throw invalid_argument("ore,minuti e secondi non validi!!");}
}//fine del settaggio del tempo

void time::printUniversal(){
    cout << ore << ":" << minuti << ":" << secondi;
} //fine della stampa

void time::printStandard(){
    cout << ((ore == 0 || ore == 12) ? 12 : ore % 12) <<  ":" << setfill('0') << minuti << ":" << secondi << (ore < 12 ? " AM: ": " PM");
} // fine della stampa standard

//time.h
#ifndef TIME_H
#define TIME_H

class time{
public:
    time();
    void setTime(int,int,int);
    void printUniversal();
    void printStandard();
private:
    int ore;
    int minuti;
    int secondi;
};

#endif /* TIME_H */

1 个答案:

答案 0 :(得分:0)

作为commenters have alluded to,您的班级time位于全局namespace中(即它不属于自己的namespace),因此引用它的正确方法是用:

int main()
{
    time t;
}