错误:ISO C++禁止声明无类型的'TimerExeption'

error: ISO C++ forbids declaration of 'TimerExeption' with no type

本文关键字:TimerExeption 类型 ISO C++ 禁止 声明 错误      更新时间:2023-10-16

当我试图在timer.cpp文件中抛出TimerException类型的异常时,我得到了这个错误。下面是timer_exception.h

  1 #ifndef TIMER_EXCEPTION_H
  2 #define TIMER_EXCEPTION_H
  3                              
  4 #include <iostream>
  5 #include <string>   
  6                                                                        
  7 class TimerException{         
  8         friend std::ostream &operator <<(std::ostream &os, const TimerException e){
  9                 std::cout << " *** TIMER EXCEPTION *** " << e.message;
 10                 return os;    
 11         }                                
 12 public:                         
 13         TimerExeption(std::string message) : message(message) {}
 14 private:                        
 15         std::string message;                   
 16 };                                         
 17                       
 18                                 
 19 #endif   

这里是我的timer.cpp文件,其中TimerException正在被实例化

  1 #include <ctime>
  2 #include "timer.h"
  3 #include "timer_exception.h" 
  4 
  5 void Timer::start(){
  6         if(timer != 0) throw TimerException("Timer already started");
  7         this->timer = clock();
  8 }       

简单的排版错误。你的构造函数名称中缺少了一个'c'。

13         TimerExeption(std::string message) : message(message) {}
//               ^^^

构造函数有一个错别字。timerexception, c缺失