gpt4 book ai didi

c++ - 在另一个类 C++ 中用作成员的类的 header 包含问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:14 25 4
gpt4 key购买 nike

我的类定义有问题,可能是因为包含顺序或其他原因。我收到的错误消息是

g++ -I/opt/PDmesa/Mesa-5.0.1/include -I/opt/PDmesa/GLUT-3.7/include -c test.cpp
In file included from prog.h:16,
from test.cpp:10:
repeat.h:21: error: ‘Prog’ does not name a type
repeat.h:27: error: ‘Prog’ has not been declared
repeat.h: In constructor ‘Repeat::Repeat(float)’:
repeat.h:34: error: ‘in’ was not declared in this scope
repeat.h:34: error: ‘pg’ was not declared in this scope
repeat.h: In member function ‘virtual void Repeat::Run()’:
repeat.h:44: error: ‘class Repeat’ has no member named ‘pg’
make: *** [test.o] Error 1
%

所以问题是我应该怎么做才能同时使用我的两个类(class)? main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <iterator>
#include "prog.h"
#include "window.h"


using namespace std;

Prog Turtle;

void draw(void)
{
Turtle.Run();
}


int main ( int argc, char** argv ) // Create Main Function For Bringing It All Together
{
filebuf fb;
fb.open (argv[1],ios::in);
istream input(&fb);
input>>Turtle;
fb.close();
window w(argc,argv);
}

程序.h

#ifndef PROG_H
#define PROG_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <iterator>
#include "forward.h"
#include "left.h"
#include "right.h"
#include "jump.h"
#include "repeat.h"

using namespace std;

class Prog
{
private:

public:
Prog();
~Prog();
void Run();
void clearbuff();
vector<node*> listing;
friend istream& operator>> (istream& in, Prog& pro);
};


Prog::Prog()
{
//Default constructor
}

Prog::~Prog()
{
//Default destructor
}

void Prog::Run()
{
size_t sz=this->listing.size();
for (size_t it=0;it<sz;it++)
{
node* ptr = this->listing.at(it);
ptr->Run();
}
}

void Prog::clearbuff()
{
size_t sz=this->listing.size();
for (size_t it=0;it<sz;it++)
{
node* ptr = this->listing.at(it);
delete ptr;
}
}

istream& operator>> (istream& in, Prog& pro)
{
string tmp, command;
double value;
vector<string> text;
while (in>>tmp)
{
for (size_t i=0;i!=tmp.size()+1;++i)
tmp[i]=toupper(tmp[i]);
text.push_back(tmp);
}
while (!text.empty())
{
command=text[0];
istringstream inpStream(text[1]);
float value = 0.0;
if ((inpStream >> value)&&!text.empty())
{
if (command=="REPEAT")
{
unsigned int x(1), y(0), i(1), pos (0);
text.erase (text.begin(), text.begin()+2);
vector<string> reptext;
if (text[0]=="[")
{
for (i=1;(x!=y)&&i<=text.size();i++)
{
if (text[i]=="[")
++x;
else if (text[i]=="]")
++y;
reptext.push_back(text[i]);
pos=i;
}
reptext.erase(reptext.begin()+pos-1,reptext.end());
ofstream tempfile ("output.txt");
for(i=0; i<reptext.size(); i++)
tempfile << reptext[i] << endl;
tempfile.close();
filebuf rfb;
rfb.open ("output.txt",ios::in);
istream rin(&rfb);
pro.listing.push_back(new Repeat(value));
Prog ptm;
rin>>ptm;
rfb.close();
text.erase (text.end());
text.erase (text.begin(), text.begin()+3);
}
else
cout << "not a bracket found after repeat command --problemo";
}
else if (command=="FORWARD")
{
pro.listing.push_back(new Forward(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="LEFT")
{
pro.listing.push_back(new Left(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="RIGHT")
{
pro.listing.push_back(new Right(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="JUMP")
{
pro.listing.push_back(new Jump(value));
text.erase (text.begin(), text.begin()+2);
}
else
cout << "Unknown command found in the input file!";
// text.erase(text.begin());
}
else
{
cout << " Value after command was not numeric or end of input file was reached!";
}
}
return in;
}

#endif // PROG_H

重复.h

#ifndef REPEAT_H
#define REPEAT_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <iterator>
#include "command.h"
#include "prog.h"

using namespace std;

class Repeat : public command
{
private:
Prog pg;
float repval;
public:
Repeat(float value);
~Repeat();
void Run();
friend istream& operator>> (istream& in, Prog& pro);
};

Repeat::Repeat(float value) : command(value)
{
this->repval=value;

for (int i=0;i<value;++i)
in>>pg; //ctor
}

Repeat::~Repeat()
{

}

void Repeat::Run()
{
this->pg.Run();
}

#endif // REPEAT_H

你好,我为每个需要一个的 header 添加了单独的 .cpp。现在我收到以下错误

% make -f makefile3
g++ -I/opt/PDmesa/Mesa-5.0.1/include -I/opt/PDmesa/GLUT-3.7/include -c test.cpp
g++ -c forward.cpp
g++ -c left.cpp
g++ -c right.cpp
g++ -c jump.cpp
g++ -c repeat.cpp
g++ -c prog.cpp
g++ test.o -L/opt/PDmesa/Mesa-5.0.1/lib -L/opt/PDmesa/GLUT-3.7/lib -L/usr/X11R6/lib -lglut -lGLU -lGL -lX11 -lXext -lXmu -lXi -lm -o test
test.o: In function `draw()':
test.cpp:(.text+0x2ad): undefined reference to `Prog::Run()'
test.o: In function `main':
test.cpp:(.text+0x33a): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Prog&)'
test.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x443): undefined reference to `Prog::Prog()'
test.cpp:(.text+0x448): undefined reference to `Prog::~Prog()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

测试.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include "prog.h"
#include "window.h"
using namespace std;

Prog Turtle;

void draw(void)
{
Turtle.Run();
// Turtle.clearbuff();
}


int main ( int argc, char** argv ) // Create Main Function For Bringing It All Together
{
filebuf fb;
fb.open (argv[1],ios::in);
istream input(&fb);
input>>Turtle;
fb.close();
window w(argc,argv);
}

程序.cpp

#include "prog.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include "forward.h"
#include "left.h"
#include "right.h"
#include "jump.h"
#include "repeat.h"

Prog::Prog()
{

}

Prog::~Prog()
{

}

void Prog::Run()
{
size_t sz=this->listing.size();
for (size_t it=0;it<sz;it++)
{
node* ptr = this->listing.at(it);
ptr->Run();
}
}

void Prog::clearbuff()
{
size_t sz=this->listing.size();
for (size_t it=0;it<sz;it++)
{
node* ptr = this->listing.at(it);
delete ptr;
}
}

istream& operator>> (istream& in, Prog& pro)
{
string tmp, command;
double value;
vector<string> text;
while (in>>tmp)
{
for (size_t i=0;i!=tmp.size()+1;++i)
tmp[i]=toupper(tmp[i]);
text.push_back(tmp);
}
while (!text.empty())
{
command=text[0];
istringstream inpStream(text[1]);
float value = 0.0;
if ((inpStream >> value)&&!text.empty())
{
if (command=="REPEAT")
{
unsigned int x(1), y(0), i(1), pos (0);
text.erase (text.begin(), text.begin()+2);
vector<string> reptext;
if (text[0]=="[")
{
for (i=1;(x!=y)&&i<=text.size();i++)
{
if (text[i]=="[")
++x;
else if (text[i]=="]")
++y;
reptext.push_back(text[i]);
pos=i;
}
reptext.erase(reptext.begin()+pos-1,reptext.end());
ofstream tempfile ("output.txt");
for(i=0; i<reptext.size(); i++)
tempfile << reptext[i] << endl;
tempfile.close();
filebuf rfb;
rfb.open ("output.txt",ios::in);
istream rin(&rfb);
//pro.listing.push_back(new Repeat(value,rin));
Prog ptm;
rin>>ptm;
rfb.close();
for (int rp=0;rp<value;rp++)
{
cout << rp << endl;
for (i=0;i<ptm.listing.size();i++)
pro.listing.push_back(ptm.listing.at(i));
}
text.erase (text.end());
text.erase (text.begin(), text.begin()+3);
}
else
cout << "not a bracket found after repeat command --problemo";
}
else if (command=="FORWARD")
{
pro.listing.push_back(new Forward(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="LEFT")
{
pro.listing.push_back(new Left(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="RIGHT")
{
pro.listing.push_back(new Right(value));
text.erase (text.begin(), text.begin()+2);
}
else if (command=="JUMP")
{
pro.listing.push_back(new Jump(value));
text.erase (text.begin(), text.begin()+2);
}
else
cout << "Unknown command found in the input file!";
// text.erase(text.begin());
}
else
{
cout << " Value after command was not numeric or end of input file was reached!";
}
}
return in;
}

程序.h

#ifndef PROG_H
#define PROG_H

#include <iostream>
#include <fstream>
#include <vector>
#include "node.h"
using namespace std;

class Repeat;

class Prog
{
private:

public:
Prog();
~Prog();
void Run();
void clearbuff();
friend istream& operator>> (istream& in, Prog& pro);
vector<node*> listing;
};

#endif // PROG_H

重复.cpp

#include "repeat.h"
using namespace std;

Repeat::Repeat(float value, istream in) : command(value)
{
this->repval=value;
for (int i=0;i<value;++i)
in>>pg; //ctor
}

Repeat::~Repeat()
{

}

void Repeat::Run()
{
this-> pg.Run();
}

重复.h

#ifndef REPEAT_H
#define REPEAT_H

#include <iostream>
#include <fstream>
#include "command.h"
#include "prog.h"
using namespace std;

class Prog;

class Repeat : public command
{
private:
Prog pg;
float repval;
public:
Repeat(float value, istream in);
~Repeat();
void Run();
friend istream& operator>> (istream& in, Prog& pro);
};

#endif // REPEAT_H

如果我从测试中删除 #include "prog.h" 和对 Prog 的所有引用,它可以正确编译,但实际上不起作用。我实际上还想做的是从 prog.cpp 中取消注释 pro.listing.push_back(new Repeat(value,rin)); 并删除接下来的 10 行。这一行是前一行的问题设计。我怀疑我又对整个 header 做错了什么

最佳答案

这里的问题是你有循环引用。

您可以将 #include 语句视为简单地将该文件的内容剪切并粘贴到指令所在的文件中。您可以做几件事。

#1 - 将您的实现放在“.cpp”文件中

您的“Prog”类定义没有提及Repeat。如果您有一个包含实际方法定义的“prog.cpp”文件,您可以在那里#include "repeat.h",这样就不会有任何问题。

其他技巧包括前向声明,在需要发生循环引用的类定义中使用指针等。

关于c++ - 在另一个类 C++ 中用作成员的类的 header 包含问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4429154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com