使用头文件自学——求教
Self-taught by using header files - Seeking advices
我正在自学如何将头文件与 .cpp 文件一起使用。我已经在这个问题上工作了一段时间,但无法弄清楚。谁能帮我解决两个错误?谢谢:)
driver.cpp
#include <cstdlib>
using namespace std;
#include "F.h"
#include "G.h"
int main()
{
FMMoore::hello();
GMMoore::hello();
system("pause");
return 0;
}
F.cpp
#include <iostream>
using std::cout;
#include "F.h"
namespace FMMoore
{
void hello()
{
cout << "hello from f.\n";
}
}
F.h
#ifndef F_H
#define F_H
namespace FMMoore
{
class FClass
{
public:
void hello();
};
}
#endif // F_H
G.cpp
#include <iostream>
using std::cout;
#include "G.h"
namespace GMMoore
{
void hello()
{
cout << "hello from g.\n";
}
}
G.h
#ifndef G_H
#define G_H
namespace GMMoore
{
class GClass
{
public:
void hello();
};
}
#endif // G_H
错误是 'hello' 不是 'FMMoore' 的成员并且 'GMMoore' 尚未声明。
我也一直在检查拼写错误和其他事情。我不知道为什么它没有宣布。
在F.h
中,hello
被声明为FClass
的成员函数,定义在FMMoore
下] 命名空间:
#ifndef F_H
#define F_H
namespace FMMoore
{
class FClass
{
public:
void hello();
};
}
#endif // F_H
但是,在 F.cpp
中,您在 FMMoore
命名空间下实现了一个函数 hello
,但该函数 不是 的成员函数FClass
:
namespace FMMoore
{
void hello()
{
cout << "hello from f.\n";
}
}
与 G.h
/G.cpp
类似。
根据您在 driver.cpp
中的代码:
FMMoore::hello();
GMMoore::hello();
听起来你想要一个自由函数 hello
(不是 class 成员函数),所以你应该修复 headers,例如F.h
:
#ifndef F_H
#define F_H
namespace FMMoore
{
// hello is a free function under the FMMoore namespace
void hello();
}
#endif // F_H
我正在自学如何将头文件与 .cpp 文件一起使用。我已经在这个问题上工作了一段时间,但无法弄清楚。谁能帮我解决两个错误?谢谢:)
driver.cpp
#include <cstdlib>
using namespace std;
#include "F.h"
#include "G.h"
int main()
{
FMMoore::hello();
GMMoore::hello();
system("pause");
return 0;
}
F.cpp
#include <iostream>
using std::cout;
#include "F.h"
namespace FMMoore
{
void hello()
{
cout << "hello from f.\n";
}
}
F.h
#ifndef F_H
#define F_H
namespace FMMoore
{
class FClass
{
public:
void hello();
};
}
#endif // F_H
G.cpp
#include <iostream>
using std::cout;
#include "G.h"
namespace GMMoore
{
void hello()
{
cout << "hello from g.\n";
}
}
G.h
#ifndef G_H
#define G_H
namespace GMMoore
{
class GClass
{
public:
void hello();
};
}
#endif // G_H
错误是 'hello' 不是 'FMMoore' 的成员并且 'GMMoore' 尚未声明。
我也一直在检查拼写错误和其他事情。我不知道为什么它没有宣布。
在F.h
中,hello
被声明为FClass
的成员函数,定义在FMMoore
下] 命名空间:
#ifndef F_H #define F_H namespace FMMoore { class FClass { public: void hello(); }; } #endif // F_H
但是,在 F.cpp
中,您在 FMMoore
命名空间下实现了一个函数 hello
,但该函数 不是 的成员函数FClass
:
namespace FMMoore { void hello() { cout << "hello from f.\n"; } }
与 G.h
/G.cpp
类似。
根据您在 driver.cpp
中的代码:
FMMoore::hello(); GMMoore::hello();
听起来你想要一个自由函数 hello
(不是 class 成员函数),所以你应该修复 headers,例如F.h
:
#ifndef F_H
#define F_H
namespace FMMoore
{
// hello is a free function under the FMMoore namespace
void hello();
}
#endif // F_H