살려주세요!! C++ 클래스가 원하는대로 작동되지 않아요!!

MyAbby의 이미지

안녕하세여.

mysql c api를 php의 mysqli 처럼 만들어 서버프로그램에 활용해 보려고 mysqli 짝퉁 클래스를 만들고 있습니다.
근데 뭐... 하루도 안지나서 휴지장이 되었습니다...

생성자 인수는
1. 아무것도 없음: 로컬 서버로 연결
2. 호스트, 유저, 암호, DB이름: 서버 연결 (포트 3306)
3. 호스트, 유저, 암호, DB이름, 포트번호: 서버 연결

DBINFO: 연결 정보를 담아두기 위한 구조체
errstr: 에러 문자열
errnum: 에러 번호
connection_time: 연결 시간제한
session: 개체가 연결한 서버 연결정보
dbcon: mysql api 포인터

real_connect(): 연결 정보를 갖고 실제 서버의 연결 시도
update_errinfo(): 에러 업데이트

생성자의 인수를 제대로 주면 서버 연결은 잘 합니다.
문제는 에러 업데이트에요. 제가 원하는 건 연결 에러가 생기면 errnum에 에러 번호가 들어있어야 합니다.
확실히 생성시 update_errinfo()가 호출이 되는데, 외부에서 참조해 보면 0이 들어가 있습니다!

정적, 동적 두가지 생성 모두 0입니다... 도와주세요... 이거 없으면 이 클래스 더이상 못만듭니다... 쿼리 에러도 저렇게 처리할 건데...

#include <mysql.h>
 
class mysqli
{
	public:
		struct DBINFO
		{
			char addr[40], user[128], pw[128], db[128];
			short port;
		};
		char errstr[128];
		int errnum;
		unsigned int connection_time;
		bool hasConnected;
		DBINFO session;
		MYSQL dbcon;
 
		void update_errinfo()
		{
			errnum = mysql_errno(&dbcon);
			sprintf(errstr, "MYSQL ERRNO %u: %s", errnum, mysql_error(&dbcon));
		}
		bool real_connect(DBINFO xInfo)
		{
			if(mysql_init(&dbcon) == NULL)
			{
				return false;
			}
			if(!mysql_real_connect(&dbcon, xInfo.addr, xInfo.user, xInfo.pw, xInfo.db, xInfo.port, 0, 0))
			{
	       			return false;
			}
			if(mysql_set_character_set(&dbcon, "utf8"))
			{
				return false;
			}
 
			hasConnected = true;
			return true;
		}
		mysqli()
		{
			mysqli("", "", "", "");
		}
		mysqli(const char *xAddr, const char *xUser, const char *xPw, const char *xDb)
		{
			mysqli(xAddr, xUser, xPw, xDb, 3306);
		}
		mysqli(const char *xAddr, const char *xUser, const char *xPw, const char *xDb, const short xPort)
		{
			memset(errstr, 0, sizeof(errstr));
			errnum = 0;
			connection_time = 5;
			hasConnected = false;
 
			strcpy(session.addr, xAddr);
			strcpy(session.user, xUser);
			strcpy(session.pw, xPw);
			strcpy(session.db, xDb);
			session.port = xPort;
 
			real_connect(session);
 
			update_errinfo();
		}
};
MyAbby의 이미지

g++ -o main main.cpp $(mysql_config --libs) $(mysql_config --cflags)

이렇게 컴파일 했습니다.

update_errinfo()함수 내에서 errnum을 출력해 보면 에러번호가 제대로 있습니다. 멤버변수가 2개가 만들어 졌나?

klara의 이미지

생성자 안에서 다른 생성자에 위임하려면 C++11의 위임 생성자를 사용해야합니다.
생성자 안에서 그냥 생성자를 호출하는 건, 그 생성자로 임시 객체를 하나 만드는겁니다.
C++11의 위임생성자를 쓸 수 없다면, 생성자마다 일일이 초기화를 따로 해주거나, 초기화전용 함수를 하나 만들어서 생성자에서 초기화함수를 호출해야합니다.

MyAbby의 이미지

mysqli 한개만 남겨두고도 안됬었는데....

update 메서드를 불러주니 제대로 되는군요.

정말 감사해요!1

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.