error: multiple declarations `xalloc' and `std::exception'
글쓴이: puaxx / 작성시간: 목, 2005/11/24 - 9:54오전
컴파일 하면 아래와 같이 에러가 나네요..왜 그런지 모르겠습니다.
c++ -fexceptions -c eg.cc
eg.cc: In function `int main(int, char**)':
eg.cc:43: error: multiple declarations `xalloc' and `std::exception'
#include <new>
#include <iostream>
#include <fstream>
#include "regexpr.h"
using namespace std;
class xalloc {} _xa;
void new_error()
{
throw _xa;
}
int main(int argc, char *argv[])
{
if(argc != 2 && argc != 3) {
cerr << "Usage: " << argv[0] << " pattern [file]" << endl;
return 1;
}
ifstream in(0);
if(argc == 3) {
in.close();
in.open(argv[2]);
if(!in) {
cerr << argv[0] << ": Can't open " << argv[2] << endl;
return 1;
}
}
set_new_handler(new_error);
RegExpr re;
try {
re.compile(argv[1]);
int n;
char buffer[500];
while(!in.eof()) {
in.getline(buffer, 500);
n = re.search(buffer);
if(n >= 0)
cerr << buffer << endl;
}
}
catch(xalloc exception) {
cerr << endl << "Not enough memory." << endl;
re.clear_after_error();
return 1;
}
catch(xsyntax syntax) {
cerr << endl << "Syntax error near character position "
<< syntax.getErrorPos() << endl;
re.clear_after_error();
return 1;
}
return 0;
}
Forums:


컴파일러의 잘못으로 보입니다. using 지시어 때문에 catch(xal
컴파일러의 잘못으로 보입니다. using 지시어 때문에 catch(xalloc exception)의
exception을 std::exception으로 인식하기 때문인 것 같군요. catch(xalloc e)
정도로 고치면 되겠습니다.
그리고 성능상의 이유로 예외를 값으로 받기보다는 catch(const xalloc& e)와 같이
const 참조형으로 받는 것이 좋습니다. 최적화의 여지가 더 커지니까요.
댓글 달기