为什么编译器不喜欢所有与 "Iter" 关联的字符串?
Why doesn't the compiler like all the strings that are associated with "Iter"?
我正在实施一种行为设计模式。
编译器在与“Iter”相关的行中给出错误。我完全不明白为什么会这样,更不知道如何解决?
我以为可能哪里有笔误(我正在按照书上的例子做程序,代码是截图,需要重新输入)——但没有笔误,一切都是完全一样。
那么可能是什么问题?
我怀疑这是一些简单的事情,但由于我的知识水平低,我无法立即理解。
错误:
main.cpp:57:2: error: ‘Iter’ does not name a type
Iter* createIterator() const;
^~~~
main.cpp:98:41: error: no ‘Iter* ContainerPerson::CreateIterator() const’ member function declared in class ‘ContainerPerson’
Iter* ContainerPerson::CreateIterator() const {
^~~~~
main.cpp: In function ‘int main()’:
main.cpp:113:18: error: ‘class ContainerPerson’ has no member named ‘createIterator’
Iter* it = list.createIterator();
^~~~~~~~~~~~~~
main.cpp:118:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->next()) {
~~~~^~~~~
main.cpp:69:7: note: declared here
void first() {
^~~~~
main.cpp:124:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->prev()) {
~~~~^~~~~
main.cpp:69:7: note: declared here
void first() {
^~~~~
完整代码:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
string address;
Person() {
name = "";
age = 0;
address = "";
}
Person(string n, int a, string ad) {
name = n;
age = a;
address = ad;
}
void prn() {
cout << "Информация о человеке:";
cout << "\n Фамилия: " << name;
cout << "\t Возраст: " << age;
cout << "\n Город: " << address;
}
};
class ContainerPerson {
public:
Person* masPerson;
int currentpos;
ContainerPerson(int size) {
masPerson = new Person[size];
currentpos = - 1;
}
~ContainerPerson() {
delete []masPerson;
}
void add(Person *obj) {
int pos = ++currentpos;
masPerson[pos].name = obj->name;
masPerson[pos].age = obj->age;
masPerson[pos].address = obj->address;
}
bool isEmpty() {
return (currentpos == - 1);
}
friend class Iter;
Iter* createIterator() const;
};
class Iter {
const ContainerPerson* container;
int index;
public:
Iter(const ContainerPerson* con) {
container = con;
}
void first() {
index = 0;
}
void end() {
index = container->currentpos;
}
void next() {
index++;
}
void prev() {
index--;
}
bool isDoneEnd() {
return (index == container->currentpos + 1);
}
bool isDoneBegin() {
return (index == -1);
}
Person currentItem() {
return container->masPerson[index];
}
};
Iter* ContainerPerson::CreateIterator() const {
return new Iter(this);
}
int main() {
setlocale(LC_ALL, "Russian");
ContainerPerson list(7);
list.add(new Person("Sur1", 26, "City1"));
list.add(new Person("Sur2", 33, "City2"));
list.add(new Person("Sur3", 65, "City3"));
list.add(new Person("Sur4", 43, "City4"));
list.add(new Person("Sur5", 58, "City5"));
list.add(new Person("Sur6", 47, "City6"));
list.add(new Person("Sur7", 32, "City7"));
Iter* it = list.createIterator();
Person currentPerson;
cout << "\nOutput (beg-next):";
for(it->first; !it->isDoneBegin(); it->next()) {
currentPerson = it->currentItem();
currentPerson.prn();
}
cout << "\nOutput (beg-prev):";
for(it->first; !it->isDoneBegin(); it->prev()) {
currentPerson = it->currentItem();
currentPerson.prn();
}
return 0;
}
您需要在 class ContainerPerson
声明之外的前向声明 class Iter;
。否则朋友 class 是 ContainerPerson::Iter
与您稍后声明的 Iter
无关。参见 Why does a C++ friend class need a forward declaration only in other namespaces?
Iter* ContainerPerson::CreateIterator()
定义中的拼写错误:应为 createIterator
,小写 c
。
for(it->first; ...)
中的错别字;应该是 it->first()
.
您的迭代混淆了。他们应该是
for(it->first(); !it->isDoneEnd(); it->next()) {
// ...
}
for(it->end(); !it->isDoneBegin(); it->prev()) {
// ...
}
修复这些后,它可以干净地编译和运行:https://godbolt.org/z/3K7T7d14h
额外的错误供您稍后修复:
你永远不会 delete
在 ContainerPerson::createIterator()
中分配的 Iter
。那是内存泄漏。
main.cpp:57:2: error: ‘Iter’ does not name a type
Iter* createIterator() const;
^~~~
虽然是朋友的声明class
friend class Iter;
出现在 class ContainerPerson
的定义中,但是名称 Iter
在 class ContainerPerson
的范围内不可见。
因此编译器针对该成员函数声明发出错误。
您需要在 class ContainerPerson
之前声明 class Iter
。例如
class Iter;
class ContainerPerson
{
//...
friend class Iter;
Iter* createIterator() const;
//...
};
或者在函数声明中的 return 类型中使用详细的类型说明符,如
class ContainerPerson
{
//...
friend class Iter;
class Iter* createIterator() const;
//...
};
main.cpp:98:41: error: no ‘Iter* ContainerPerson::CreateIterator() const’ member function declared in class ‘ContainerPerson’
Iter* ContainerPerson::CreateIterator() const {
^~~~~
你声明了成员函数
Iter* createIterator() const;
^^
不是成员函数
Iter* CreateIterator() const;
^^
main.cpp: In function ‘int main()’:
main.cpp:113:18: error: ‘class ContainerPerson’ has no member named ‘createIterator’
Iter* it = list.createIterator();
^~~~~~~~~~~~~~
此错误是第一个错误的结果。
至于这些错误
main.cpp:118:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->next()) {
~~~~^~~~~
和
main.cpp:124:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->prev()) {
~~~~^~~~~
那么你的意思好像是函数调用
it->first()
我正在实施一种行为设计模式。
编译器在与“Iter”相关的行中给出错误。我完全不明白为什么会这样,更不知道如何解决?
我以为可能哪里有笔误(我正在按照书上的例子做程序,代码是截图,需要重新输入)——但没有笔误,一切都是完全一样。
那么可能是什么问题?
我怀疑这是一些简单的事情,但由于我的知识水平低,我无法立即理解。
错误:
main.cpp:57:2: error: ‘Iter’ does not name a type
Iter* createIterator() const;
^~~~
main.cpp:98:41: error: no ‘Iter* ContainerPerson::CreateIterator() const’ member function declared in class ‘ContainerPerson’
Iter* ContainerPerson::CreateIterator() const {
^~~~~
main.cpp: In function ‘int main()’:
main.cpp:113:18: error: ‘class ContainerPerson’ has no member named ‘createIterator’
Iter* it = list.createIterator();
^~~~~~~~~~~~~~
main.cpp:118:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->next()) {
~~~~^~~~~
main.cpp:69:7: note: declared here
void first() {
^~~~~
main.cpp:124:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->prev()) {
~~~~^~~~~
main.cpp:69:7: note: declared here
void first() {
^~~~~
完整代码:
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
string address;
Person() {
name = "";
age = 0;
address = "";
}
Person(string n, int a, string ad) {
name = n;
age = a;
address = ad;
}
void prn() {
cout << "Информация о человеке:";
cout << "\n Фамилия: " << name;
cout << "\t Возраст: " << age;
cout << "\n Город: " << address;
}
};
class ContainerPerson {
public:
Person* masPerson;
int currentpos;
ContainerPerson(int size) {
masPerson = new Person[size];
currentpos = - 1;
}
~ContainerPerson() {
delete []masPerson;
}
void add(Person *obj) {
int pos = ++currentpos;
masPerson[pos].name = obj->name;
masPerson[pos].age = obj->age;
masPerson[pos].address = obj->address;
}
bool isEmpty() {
return (currentpos == - 1);
}
friend class Iter;
Iter* createIterator() const;
};
class Iter {
const ContainerPerson* container;
int index;
public:
Iter(const ContainerPerson* con) {
container = con;
}
void first() {
index = 0;
}
void end() {
index = container->currentpos;
}
void next() {
index++;
}
void prev() {
index--;
}
bool isDoneEnd() {
return (index == container->currentpos + 1);
}
bool isDoneBegin() {
return (index == -1);
}
Person currentItem() {
return container->masPerson[index];
}
};
Iter* ContainerPerson::CreateIterator() const {
return new Iter(this);
}
int main() {
setlocale(LC_ALL, "Russian");
ContainerPerson list(7);
list.add(new Person("Sur1", 26, "City1"));
list.add(new Person("Sur2", 33, "City2"));
list.add(new Person("Sur3", 65, "City3"));
list.add(new Person("Sur4", 43, "City4"));
list.add(new Person("Sur5", 58, "City5"));
list.add(new Person("Sur6", 47, "City6"));
list.add(new Person("Sur7", 32, "City7"));
Iter* it = list.createIterator();
Person currentPerson;
cout << "\nOutput (beg-next):";
for(it->first; !it->isDoneBegin(); it->next()) {
currentPerson = it->currentItem();
currentPerson.prn();
}
cout << "\nOutput (beg-prev):";
for(it->first; !it->isDoneBegin(); it->prev()) {
currentPerson = it->currentItem();
currentPerson.prn();
}
return 0;
}
您需要在
class ContainerPerson
声明之外的前向声明class Iter;
。否则朋友 class 是ContainerPerson::Iter
与您稍后声明的Iter
无关。参见 Why does a C++ friend class need a forward declaration only in other namespaces?Iter* ContainerPerson::CreateIterator()
定义中的拼写错误:应为createIterator
,小写c
。for(it->first; ...)
中的错别字;应该是it->first()
.您的迭代混淆了。他们应该是
for(it->first(); !it->isDoneEnd(); it->next()) {
// ...
}
for(it->end(); !it->isDoneBegin(); it->prev()) {
// ...
}
修复这些后,它可以干净地编译和运行:https://godbolt.org/z/3K7T7d14h
额外的错误供您稍后修复:
你永远不会 delete
在 ContainerPerson::createIterator()
中分配的 Iter
。那是内存泄漏。
main.cpp:57:2: error: ‘Iter’ does not name a type
Iter* createIterator() const;
^~~~
虽然是朋友的声明class
friend class Iter;
出现在 class ContainerPerson
的定义中,但是名称 Iter
在 class ContainerPerson
的范围内不可见。
因此编译器针对该成员函数声明发出错误。
您需要在 class ContainerPerson
之前声明 class Iter
。例如
class Iter;
class ContainerPerson
{
//...
friend class Iter;
Iter* createIterator() const;
//...
};
或者在函数声明中的 return 类型中使用详细的类型说明符,如
class ContainerPerson
{
//...
friend class Iter;
class Iter* createIterator() const;
//...
};
main.cpp:98:41: error: no ‘Iter* ContainerPerson::CreateIterator() const’ member function declared in class ‘ContainerPerson’
Iter* ContainerPerson::CreateIterator() const {
^~~~~
你声明了成员函数
Iter* createIterator() const;
^^
不是成员函数
Iter* CreateIterator() const;
^^
main.cpp: In function ‘int main()’:
main.cpp:113:18: error: ‘class ContainerPerson’ has no member named ‘createIterator’
Iter* it = list.createIterator();
^~~~~~~~~~~~~~
此错误是第一个错误的结果。
至于这些错误
main.cpp:118:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->next()) {
~~~~^~~~~
和
main.cpp:124:10: error: invalid use of non-static member function ‘void Iter::first()’
for(it->first; !it->isDoneBegin(); it->prev()) {
~~~~^~~~~
那么你的意思好像是函数调用
it->first()