在多文件编译中,将函数的变量放置在何处

Where to place variables of a function in multifile compilation

本文关键字:变量 在何处 函数 文件 编译      更新时间:2024-09-27

我有一个应用程序,它的类和函数都在源文件中。我一直在读关于如何将它们放入专用.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;
}

修改

我做的一些修改包括:

  1. 在标头post.h中添加包含防护。这是一种推荐的做法
  2. 头文件post.h中,我们只有成员函数的声明
  3. 源文件post.cpp中,我们有对应于每个成员函数的定义

此外,我建议您使用此处列出的书籍而不是youtube视频进行学习。