C++에서 (void*) type 말고 casting용으로 뭘 써야 하나요?

aeronova의 이미지

안녕하세요,
제가 다음과 같은 마지막 인자만 다른 두 가지 함수가 있습니다.

    void printMSG(const char* msg1, const int& msg2) {
        std::cout << msg1 << " : " << msg2 << std::endl;
    }
    void printMSG(const char* msg1, const double& msg2) {
        std::cout << msg1 << " : " << msg2 << std::endl;
    }   

근데 생각해보니 인자에 따라 두가지 함수를 각각 정의하는 것보다 casting을 이용하면 하나의 함수를 사용할 수 있지 않을까 하는 생각에 마지막 인자를 (void*)로 type을 정의하니 C++에서는 금지되었다면서 에러가 나더군요. 음.. 그러면 (void*) type말고 다른 것이 있나요? 아니면 C++에서는 explicit type을 선호해서 아예 되지 않는지요?
Scarecrow의 이미지

함수인자로 정의하는 것은 문제가 아니지만...
함수내부에서 std::cout가 그 변수를
어떤 자료형으로 처리해야 할지 알 수 없으니 에러가 날듯 싶군요.

pool007의 이미지

#include <iostream>
 
using namespace std;
 
template<typename T>
void printMSG(const char* msg1, const T& msg2) {
    std::cout << msg1 << " : " << msg2 << std::endl;
}
 
int main()
{
    printMSG("hi", 1);
    printMSG("hi2", 2.0);
 
    return EXIT_SUCCESS;
}

--
Passion is like genius; a miracle.

--
Passion is like genius; a miracle.

aeronova의 이미지

앗, template이란 막강한 녀석을 깜빡했네요. :)
감사합니다.

... 그런데 파일을 debug.h, debug.cpp, debugTest.cpp로 쪼개니 이상하게 undefined reference 에러가 납니다.ㅜㅜ
한 파일안에 있을때는 되고, 왜 쪼개니 안될까요?

debug.h

#ifndef debug_H_
#define debug_H_
 
#include <iostream>
#include <iomanip>
#include <vector>
 
typedef std::vector<double> Row;
typedef std::vector<Row> Matrix;
 
template<typename T>
void printDebugMSG(const T& msg); 
 
#endif // debug_H_

debug.cpp

#include "debug.h"
#ifdef __DEBUG__
template<typename T>
void printDebugMSG(const T& msg) {
    std::cout << msg << std::endl;
}
#else
template<typename T>
void printDebugMSG(const T& msg) {
    // do nothing
}
#endif // __DEBUG__

debugTest.cpp

#include "debug.h"
int main(void) {
    printDebugMSG("Hello");
    return 0;
}

컴파일은 다음처럼 했습니다.

g++ -I./ -c debug.cpp
g++ -I./ -c debugTest.cpp
g++ debug.o debugTest.o -o debugTest

그러니 다음과 같은 에러가 납니다. -D__DEBUG__ 옵션을 주어도 마찬가지구요.

debugTest.o(.text+0x132):debugTest.cpp: undefined reference to `void printDebugM
SG<char [6]>(char const (&) [6])'
collect2: ld returned 1 exit status

휴... 생각보다 쉽지 않네요. :(

It's better to burn out than to fade away. -- Kurt Cobain.

Scarecrow의 이미지

template으로 작성할때는 정의와 구현을 구분하지 않고 구현 내용도 .h에 넣어줍니다.
실제 코드가 아니고 코드를 만드는 코드기 때문에 .h에 넣습니다.

debug.cpp에 나오는 template 함수들을 debug.h로 옮겨줍니다.

aeronova의 이미지

감사합니다. 머리 좀 식히고 googling 해보니 C++ FAQ에 잘 설명 되어 있군요.
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.14

요지는 말씀하신대로 *.h에 definition을 넣어주거나 (간단하지만 코드가 커질 우려가 있음), 아니면 *.cpp에 사용할 type을 일일이 적어줄것, 이렇게 2가지 방도가 있네요.

It's better to burn out than to fade away. -- Kurt Cobain.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.