wxDev.fr, le portail francophone consacré à wxWidgets ! ( The french portal for wxWidgets )  
Esp. membre
Recheche rapide



Recherche avancée
Statistiques
Membres inscrits :2359

Membres en ligne : 0
Invités en ligne : 3
Pub hébergeur
Pourquoi cette pub ?

Valid XHTML 1.0 Transitional

Valid CSS2

Menu forum (navigation):
Pages: 1  
 
Accueil » Accueil forums » Développement C/C++
» Container vector - Erreur de compile
Conversation (Résolue) : Container vector - Erreur de compile
27-01-2014 15:46:06  Container vector - Erreur de compile #1
Ded (Membre)
Lieu: Grenoble (38)
Inscrit le : 09-09-2013
Messages: 41
Snippets: 0
Tutoriels: 0
Hors ligne
Pas vraiment lié à wxWidgets : j'essaie de compiler ce fragment et j'ai des erreurs de compile.
J'essaie de comprendre les containers et la STL.

Code Cpp:

 
#include <iostream>
#include <vector>
 
using namespace std;
 
struct Entry {
    string name;
    int number;
};
 
void print_book(const vector<Entry>& book)
{
    for(unsigned int i = 0; i!=book.size(); ++i)
        cout << book[i] << '\n';
}
void print_book2(const vector<Entry>& book)
{
    for(const auto& x : book)
        cout << x << '\n';
}
int main()
{
    vector<Entry> phone_book = {
        {"David Hume",123456},
        {"Karl Popper",234567},
        {"Bertrand Arthur William Russell",345678},
        {"André Debayeux",456789}
    };
 
    cout << "Hello world!" << endl;
 
    print_book(phone_book);
    print_book2(phone_book);
 
    return 0;
}
 


Code:

 
 
-------------- Build: Release in entry-class (compiler: GNU GCC Compiler)---------------
 
mingw32-g++.exe -Wall -fexceptions -O2 -std=c++11 -ID:\CPP\id3lib\id3lib-3.8.3\include -c D:\CPP2\entry-class\main.cpp -o obj\Release\main.o
D:\CPP2\entry-class\main.cpp: In function 'void print_book(const std::vector<Entry>&)':
D:\CPP2\entry-class\main.cpp:14:23: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
In file included from c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/iostream:40:0,
                 from D:\CPP2\entry-class\main.cpp:1:
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/ostream:600:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Entry]'
D:\CPP2\entry-class\main.cpp: In function 'void print_book2(const std::vector<Entry>&)':
D:\CPP2\entry-class\main.cpp:19:17: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
In file included from c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/iostream:40:0,
                 from D:\CPP2\entry-class\main.cpp:1:
c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.7.1/include/c++/ostream:600:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Entry]'
D:\CPP2\entry-class\main.cpp: In function 'int main()':
D:\CPP2\entry-class\main.cpp:28:5: error: could not convert '{{"David Hume", 123456}, {"Karl Popper", 234567}, {"Bertrand Arthur William Russell", 345678}, {"Andr\37777777751 Debayeux", 456789}}' from '<brace-enclosed initializer list>' to 'std::vector<Entry>'
Process terminated with status 1 (0 minute(s), 0 second(s))
5 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 

Merci pour toute aide...
----------
En fait ça marche mieux avec book[i].name et x.name.

Oops

Dernière modification par Ded (27-01-2014 15:58:07)


Code::Blocks 13.12 on Windows 7 (build 7601 service pack 1 64 bit edition).
GNU GCC version 4.7.1 -std=c++11 Make version 3.82.
wxWidgets 3.0.0 compiled shared=0 monolithic=1 unicode=1 build=release.
Menu forum (navigation):
Pages: 1  
 
Accueil » Accueil forums » Développement C/C++
» Container vector - Erreur de compile