STL 처음시작하는데 도와주세요.

kirina70의 이미지

#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <string>
#include <iterator>
using namespace std;
 
template<typename Container>
void PrintContainer(string strTitle, Container &cont);
 
int main()
{
    int pArray[5] = {9,1,4,7,2};
    list<int> l;
    copy(pArray, pArray+5, front_inserter(l));
    PrintContainer("Post",l);
    vector<int> v;
    for(int i=0;i<5;i++)
        back_inserter(v) = pArray[i];
    PrintContainer("Back",v);
    set<int> s;
    copy(pArray,pArray+5,inserter(s,s.begin()));
    PrintContainer("Insert",s);
    return 0;
}
 
template<typename Container>
void PrintContainer(string strTitle, Container &cont)
{
    cout << strTitle << ":";
    for(Container::iterator it = cont.begin(); it != cont.end(); ++it)
    {
        cout.width(3);
        cout << *it;
    }
    cout << endl;
}

저렇게 작성했는데 에러가 납니다.

개인적으로 틀린게 없다고 생각하는데 말이죠...

책에 있는 소스 그냥 썼는데(책에는 VC.net으로 돌렸습니다.)

하지만 책에서 Standard Library의 중요성을 강조했는데

리눅스하고 VC하고 틀리면 무슨 소용일까 생각합니다.

혹시 제가 잘못한게 있을까요 빼먹은거나

<>깨질게 분명하기에 작성한 소스 올려드립니다.

글고 kldp.org에서 cpp 못올리게 해서 확장자 txt로 고쳤습니다.

밑에는 에러표시줄이요...

kirina@kirina:~/stl$ g++ first.cpp
first.cpp: In function ‘void PrintContainer(std::string, Container&)’:
first.cpp:32: error: expected `;' before ‘it’
first.cpp:32: error: ‘it’ was not declared in this scope
first.cpp: In function ‘void PrintContainer(std::string, Container&) [with Container = std::list >]’:
first.cpp:17: instantiated from here
first.cpp:32: error: dependent-name ‘Container::iterator’ is parsed as a non-type, but instantiation yields a type
first.cpp:32: note: say ‘typename Container::iterator’ if a type is meant
first.cpp: In function ‘void PrintContainer(std::string, Container&) [with Container = std::vector >]’:
first.cpp:21: instantiated from here
first.cpp:32: error: dependent-name ‘Container::iterator’ is parsed as a non-type, but instantiation yields a type
first.cpp:32: note: say ‘typename Container::iterator’ if a type is meant
first.cpp: In function ‘void PrintContainer(std::string, Container&) [with Container = std::set, std::allocator >]’:
first.cpp:24: instantiated from here
first.cpp:32: error: dependent-name ‘Container::iterator’ is parsed as a non-type, but instantiation yields a type
first.cpp:32: note: say ‘typename Container::iterator’ if a type is meant

File attachments: 
첨부파일 크기
Plain text icon first.cpp841바이트
imyejin의 이미지

그리고 문법 에러와는 관계없지만
string 같은 객체를 넘길 때는 참조를 사용하세요.
읽기 전용으로 넘기신다면 상수 참조를 사용하시고요.

템플릿 사용에 대해 좀더 알아보시고 예제를 따라 차근차근 공부하시기 바랍니다.
그리고 방금 전에 거의 똑같은 코드를 올리셔서 http://kldp.org/node/90835
다른 분들이 이미 답변해 주신 것 같은데 왜 또 올리시죠?

임예진 팬클럽 ♡예진아씨♡ http://cafe.daum.net/imyejin

[예진아씨 피카사 웹앨범] 임예진 팬클럽 ♡예진아씨♡ http://cafe.daum.net/imyejin

댓글 달기

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