C++로 Mysql 프로그래밍 하려고 하는데...
글쓴이: 불량청년 / 작성시간: 월, 2003/01/20 - 8:39오후
C코드로 되어 있는걸 C++ 문법으로, 즉, 클래스화 해서
만들려고 하는데 컴파일시 여러 오류가 나는군요.
대충 클래스는 아래와 같이 했습니다.
#ifndef _MOD_DAEMON_H_
#define _MOD_DAEMON_H_
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#define DB_HOST "localhost"
#define DB_USER "test"
#define DB_PASS "test"
#define DB_NAME "test"
using namespace std;
class Daemon
{
private:
public:
MYSQL *connection = NULL, conn;
MYSQL_RES *sql_result;
MYSQL_ROW sql_row;
Daemon();
~Daemon();
void connect_db(void);
void disconnect_db(void);
void select_table_query(void);
};
이렇게 하고 메인 함수에서
#include "mod.h"
int main(void)
{
Daemon mod;
mod.connect_db();
mod.select_table_query();
mod.disconnect_db();
return 0;
}
또한 클래스의 멤버함수 구현은 아래와 같이 했습니다.
#include "mod.h"
void Daemon::connect_db()
{
mysql_init(&conn);
connection = mysql_real_connect(&conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 0, (char *)NULL, 0);
}
void Daemon::disconnect_db()
{
mysql_close(connection);
}
void Daemon::select_table_query()
{
int stat;
char query[255];
memset(query, 0, 255);
sprintf(query, "select num from test");
stat = mysql_query(connection, query);
if(stat != 0)
return 1;
}
대충 이렇게 했는데 컴파일 하면
In file included from mod_daemon.cpp:1:
mod.h:30: ISO C++ forbids initialization of member `connection'
mod.h:30: making `connection' static
mod.h:30: ISO C++ forbids in-class initialization of non-const static
member `connection'
In file included from main.cpp:1:
mod.h:30: ISO C++ forbids initialization of member `connection'
mod.h:30: making `connection' static
mod.h:30: ISO C++ forbids in-class initialization of non-const static
member `connection'
이런식으로 에러가 나는데요. 초기화 에러인거 같은데... 아~
어떻게 해줘야 하는지요?
MySQL++ 를 사용하지 않고 할려고 하는데... C++구현시 mysql.h 헤더를
못쓰는건 아닌지요?
C++을 많이 해본게 아니라, 좀 헷갈리네용. ㅜㅜ; 가르침을...
Forums:


흠.. 자세히는 모르겠지만. 제 생각엔 에러 메시지 그대로 static
흠.. 자세히는 모르겠지만. 제 생각엔 에러 메시지 그대로 static 멤버가 아닌것을 초기화 해서 생긴 에러 같습니다.
class Daemon { private: public: MYSQL *connection = NULL, conn; MYSQL_RES *sql_result; MYSQL_ROW sql_row; Daemon(); ~Daemon(); void connect_db(void); void disconnect_db(void); void select_table_query(void); };위에서 connection 을 NULL 로 초기화를 했는데, 이것을 클래스 선언부에 하지 말고 Daemon() <- 이쪽 생성자에서 해보시길 바랍니다.
C++ 은 자세히 보지 않아 확실치가 않군요. 에러 메시지를 보고 추측해봤습니다.
Dream, Passion and Challenge..
C++로 MySQL...
전 잘 쓰고 있습니다... ^^)/.....
JAVA를 하시던 분이신가 봅니다.C++에서는 class의 일반m
JAVA를 하시던 분이신가 봅니다.
C++에서는 class의 일반member들은 선언과 동시에 초기화를 할 수 없습니다.
윗 분 말씀대로 constructor에서 해당 member들을 초기화 하셔야 합니다
댓글 달기