Attualmente sto leggendo la quarta edizione di "The C++ Programming Language" di Bjarne Stroustrup. Nelle prime parti del libro, ho trovato un utilizzo di using
assomiglia seguenti:Come utilizzare i modelli per creare alias con `using` (creazione di alias parametrizzati) in C++?
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
* vedi [**] per il programma completo e il messaggio di errore *
Questo è esattamente ciò che ho trovato nella pagina 105. Quando ho girato questo in un programma completo e ho cercato di compilarlo, g++
mi ha dato questo errore masseage:
> g++ -std=c++14 -o fnd find_all.cpp
find_all.cpp:13:15: error: expected '=' before '<' token
using Iterator<T> = typename T::iterator;
^
find_all.cpp:13:15: error: expected type-specifier before '<' token
non posso pinna d ogni problema nel codice, (Sono nuovo di C++, non riesco a trovare il problema con la mia poca conoscenza) (Più confusionario Ho trovato questo sul libro di Bjarne)
qualcuno potrebbe dirmi il motivo quel codice fa un errore?
NOTA: Tuttavia, se ho sostituito
Iterator<C>
contypename C::iterator
(vedere di seguito), funziona correttamente, non vi è alcun errore!
[**] Completa Programma e messaggio di errore:
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
// -------------------------------------------
// For the completeness I'll include my complete program here
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v) // find all occurrences of v in c
{
vector<Iterator<C>> res;
for (auto p = c.begin(); p!=c.end(); ++p)
if (∗p==v)
res.push_back(p);
return res;
}
void test()
{
string m {"Mary had a little lamb"};
for (auto p : find_all(m, 'a'))
if (*p == 'a')
cerr << "string bug!\n";
list<double> ld { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 1.1, 1.1 };
for (auto p : find_all(ld, 1.1))
if (*p == 1.1)
cerr << "list bug!\n";
vector<string> strv { "blue", "yellow", "red", "white", "orange", "blue" };
for (auto p : find_all(strv, "blue"))
if (*p == "blue")
cerr << "string vector bug!\n";
}
int main(void)
{
test();
return 0;
}
MESSAGGIO DI ERRORE:
> g++ -std=c++14 -o fnd find_all.cpp
find_all.cpp:13:15: error: expected '=' before '<' token
using Iterator<T> = typename T::iterator;
^
find_all.cpp:13:15: error: expected type-specifier before '<' token
find_all.cpp:16:8: error: 'Iterator' was not declared in this scope
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp:16:17: error: template argument 1 is invalid
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp:16:17: error: template argument 2 is invalid
find_all.cpp:16:18: error: expected unqualified-id before '>' token
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp: In function 'void test()':
find_all.cpp:30:31: error: 'find_all' was not declared in this scope
for (auto p : find_all(m, 'a'))
^
find_all.cpp:35:32: error: 'find_all' was not declared in this scope
for (auto p : find_all(ld, 1.1))
^
find_all.cpp:40:37: error: 'find_all' was not declared in this scope
for (auto p : find_all(strv, "blue"))
vi ringrazio molto, funziona benissimo ora. Sembra un errore di battitura nel libro, grazie – 0xEDD1E