c++:收到很多错误。简单的继承导致大量错误
c++ : Receiving a lot of errors. Simple inheritance resulting in a confusing amount of errors
下面是我的3个cpp文件和2个头文件。我收到了天文数字的错误,而且大多数都非常不清楚。我是 C++ 的新手,有 C#/Java 背景。
我很清楚以下可能是语法错误。提前感谢您的帮助。
Main.cpp:
#include <iostream>
#include "B.h"
#include "S.h"
using namespace std;
int main() {
B b;
S s("Jon");
return 0;
};
B.h:
#ifndef B_H
#define B_H
class B {
public:
B();
};
#endif
B.cpp:
#include <iostream>
#include <string>
#include "B.h"
#include "S.h"
using namespace std;
class B {
public:
B() {}
};
S.h:
#ifndef S_H
#define S_H
class S: public B {
public:
S(string name);
}
#endif
S.cpp:
#include <iostream>
#include <string>
#include "B.h"
#include "S.h"
using namespace std;
class S: public B {
private:
string s;
public:
S(string name) {
s = name;
}
};
这是我的一大堆错误。有点力不从心。
嗯,你的代码中有很多错误。我的建议是逐一检查这些错误并查看确定为罪魁祸首的行。
我还建议您查看如何声明 class 以及如何定义其成员。例如,B.h 和 B.cpp 都定义了一个 class B,但定义方式不同。然后S.h重新定义class B.
您的代码太糟糕了,我们无法逐个修复它。在回顾 C++ 中令您感到困惑的领域(例如声明和定义 classes 及其成员)之后,您需要重新开始。 Wikipedia has a good introduction。请记住,当定义与声明分开时,不要再次使用 class S { ... }
,而是使用 S::member
格式来引入定义。
1) 您不能在 header 中定义 class 并在某些源文件中重新定义它。
class 的(前向)声明是这样的语句:
class X;
class 的定义类似于:
class X
{
// stuff
};
每个class.
这样的定义只能出现一次
如果您不想让数据成员成为您 public 接口的一部分,您可以
- 使用 opague pointer 从 header 或
中完全隐藏它们
- 将这些成员设为私有。
B.h
#indef B_H
#define B_H
#include <string> // to be used here, so we need to include it
// not "using namespace std;" here!! *
class B
{
public:
B();
void setValues();
std::string printValues() const; // don't miss that std::
private:
std::string s, result;
float f;
int i;
bool b;
};
#endif
B.cc
#include "B.h"
B::B()
: f(), i(), b() // **
{ }
void B::setValues() { }
std::string printValues() const
{
result = s + " " + std::to_string(i) + " " +
std::to_string(f) + " " + std::to_string(b);
return result;
}
S.h
#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B
{
public:
S(std::string name);
std::string subPrint() const; // ***
};
#endif
S.cc
#include "S.h"
S::S(std::string name)
: s{name} // **
{ }
std::string subPrint () const // ***
{
return printValues() + s;
}
*: Why is “using namespace std” in C++ considered bad practice?
**: C++, What does the colon after a constructor mean?
***: Meaning of “const” last in a C++ method declaration?
2) 您必须在使用类型的任何地方包含必需的 headers。
你的 B.h
不包括但使用 string
我怀疑是指 std::string
.
下面是我的3个cpp文件和2个头文件。我收到了天文数字的错误,而且大多数都非常不清楚。我是 C++ 的新手,有 C#/Java 背景。
我很清楚以下可能是语法错误。提前感谢您的帮助。
Main.cpp:
#include <iostream>
#include "B.h"
#include "S.h"
using namespace std;
int main() {
B b;
S s("Jon");
return 0;
};
B.h:
#ifndef B_H
#define B_H
class B {
public:
B();
};
#endif
B.cpp:
#include <iostream>
#include <string>
#include "B.h"
#include "S.h"
using namespace std;
class B {
public:
B() {}
};
S.h:
#ifndef S_H
#define S_H
class S: public B {
public:
S(string name);
}
#endif
S.cpp:
#include <iostream>
#include <string>
#include "B.h"
#include "S.h"
using namespace std;
class S: public B {
private:
string s;
public:
S(string name) {
s = name;
}
};
这是我的一大堆错误。有点力不从心。
嗯,你的代码中有很多错误。我的建议是逐一检查这些错误并查看确定为罪魁祸首的行。
我还建议您查看如何声明 class 以及如何定义其成员。例如,B.h 和 B.cpp 都定义了一个 class B,但定义方式不同。然后S.h重新定义class B.
您的代码太糟糕了,我们无法逐个修复它。在回顾 C++ 中令您感到困惑的领域(例如声明和定义 classes 及其成员)之后,您需要重新开始。 Wikipedia has a good introduction。请记住,当定义与声明分开时,不要再次使用 class S { ... }
,而是使用 S::member
格式来引入定义。
1) 您不能在 header 中定义 class 并在某些源文件中重新定义它。
class 的(前向)声明是这样的语句:
class X;
class 的定义类似于:
class X
{
// stuff
};
每个class.
这样的定义只能出现一次如果您不想让数据成员成为您 public 接口的一部分,您可以
- 使用 opague pointer 从 header 或 中完全隐藏它们
- 将这些成员设为私有。
B.h
#indef B_H
#define B_H
#include <string> // to be used here, so we need to include it
// not "using namespace std;" here!! *
class B
{
public:
B();
void setValues();
std::string printValues() const; // don't miss that std::
private:
std::string s, result;
float f;
int i;
bool b;
};
#endif
B.cc
#include "B.h"
B::B()
: f(), i(), b() // **
{ }
void B::setValues() { }
std::string printValues() const
{
result = s + " " + std::to_string(i) + " " +
std::to_string(f) + " " + std::to_string(b);
return result;
}
S.h
#ifndef S_H
#define S_H
#include "B.h" // required to make B known here
#include <string> // known through B, but better safe than sorry
class S : public B
{
public:
S(std::string name);
std::string subPrint() const; // ***
};
#endif
S.cc
#include "S.h"
S::S(std::string name)
: s{name} // **
{ }
std::string subPrint () const // ***
{
return printValues() + s;
}
*: Why is “using namespace std” in C++ considered bad practice?
**: C++, What does the colon after a constructor mean?
***: Meaning of “const” last in a C++ method declaration?
2) 您必须在使用类型的任何地方包含必需的 headers。
你的 B.h
不包括但使用 string
我怀疑是指 std::string
.