在多文件编译中,将函数的变量放置在何处
Where to place variables of a function in multifile compilation
我有一个应用程序,它的类和函数都在源文件中。我一直在读关于如何将它们放入专用.cpp中的文章。h文件。但是,我注意到.h文件只包含定义,.cpp文件只包含函数的实现,而不包含数据成员。
我的问题是,如果我想在单独的文件中有类,函数的变量将在这两个文件中的哪一个中定义?
以下是一些代码信息:
类别:
class Post { //Will hold the contents of a single post.
private:
int UserID;
std::string message;
//time_t date; // Will eventually hold a time value.
public:
static void postMenu(User& currentUser, bool loggedIn) {
std::cout << "n**********************************n POST MENUn**********************************nn";
char postChoice = ' ';
std::cout << "n[P] - Post to board.n[V] - View Board.n[E] - Exit to main menu.nn>";
std::cin >> postChoice;
postChoice = toupper(postChoice);
if (postChoice == 'P') {
Post currentUserPost;//Initilize current post.
currentUserPost.loadPosts();
currentUserPost.postMessage(currentUser, loggedIn);
currentUserPost.loadPosts();
//PlaySound(TEXT("C:/Users/19097/Desktop/Programs/Registration Application/App files/Audio/ding.wav"), NULL, SND_FILENAME | SND_ASYNC); // Sound when message is posted
}
else if (postChoice == 'V') {
Post::loadPosts(); //Loads the post wall for viewing
}
else if (postChoice == 'E') {
std::cout << "nExiting..." << std::endl;
return;
}
}
char postMessage(User& currentUser, bool loggedIn) { //Will post a message to the board. Passes a User object.
if (loggedIn == false) {
std::cout << "You must be logged in to post!" << std::endl;
User::userLoginRegisterPrompt();
return 'F';
}
std::string message; //Actual message string
std::cout << "nEnter your message: ";
std::cin.ignore(); //Clears string buffer.
getline(std::cin, message);
std::ofstream post("App files/UserPosts/-Posts.txt", std::ios::app); //File with user posts.
post << currentUser.getUserName() << ": " << message << std::endl;
post.close();
logAction(currentUser.getUserName(), "Made a post");
}
static bool loadPosts() { //Will be for viewing the post history.
std::ifstream post("App files/UserPosts/-Posts.txt");
std::string line;
if (!post) {
std::cout << "No board exists!" << std::endl;
return false;
}
std::cout << "n**********************************n GLOBAL POSTn**********************************nn";
while (getline(post, line)) {
std::cout << line << std::endl;
}
std::cout << "nn**********************************n**********************************nn";
}
};
这个类的.cpp和.h文件看起来怎么样?我看到一个示例,其中声明是在.h文件中声明的,所有函数体都留在.cpp文件中。
然而,他们指出,所有数据成员和类/静态关键字都应该从.cpp文件中删除。
以下是教程供参考:https://www.youtube.com/watch?v=nbd7o8iKh9Q&ab_channel=日历货币
这个类的.cpp和.h文件看起来怎么样?
您可以将其用作参考。
post.h
#ifndef POST_H //ALWAYS USE INCLUDE GUARDS IN HEADER FILES
#define POST_H
#include <string>
class Post {
private:
int UserID;
std::string message;
public:
//declarations for member functions
static void postMenu(User& currentUser,bool loggedIn);
char postMessage(User& currentUser,bool loggedIn);
static bool loadPosts();
};
#endif
post.cpp
#include "post.h"
#include <fstream>
#include <iostream>
//dont forget to include the header that has User class
//this is implementation of the static member function postMenu
void Post::postMenu(User& currentUser, bool loggedIn) {
//add code here
}
//this is implementation of the non static member function postMessage
char Post::postMessage(User& currentUser, bool loggedIn) { //Will post a message to the board. Passes a User object.
//add code here
return 'Q';
}
//this is implementation of the static member function loadPosts
bool Post::loadPosts() {
//add code here
return true;
}
修改
我做的一些修改包括:
- 在标头
post.h
中添加包含防护。这是一种推荐的做法 - 在头文件
post.h
中,我们只有成员函数的声明 - 在源文件
post.cpp
中,我们有对应于每个成员函数的定义
此外,我建议您使用此处列出的书籍而不是youtube视频进行学习。
相关文章:
- 通过 get-Method 访问变量在类外不起作用
- 变量在使用赋值语句赋值后恢复为以前的值
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- 为什么thread_local变量在这里从未初始化?
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 变量如何以及在何处可能没有C++关联的名称?
- 成员变量在多线程 C++ 时自行更改
- C++变量在调用 x64 程序集函数后重置为 0
- 指针变量在 cout 函数中不起作用
- 这个变量在 C++ 中的范围是什么?
- c++ 变量在宏的扩展中没有在这个范围内声明
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 为什么枚举变量在这里是右值?
- 全局变量在 C++ 中是否显着提高了编译速度?
- 声明一个变量,该变量在 c++ 或 c 中具有值,当程序终止时不会被销毁
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 在何处放置与类方法相关的帮助程序函数C++
- 将删除运算符放置在何处以进行动态变量
- 在C++中使用头文件时如何以及在何处定义类变量
- c++中在何处初始化Class的成员变量