C++ C++;?

C++ C++;?,c++,C++,对于我的作业,它说我要使用main.cpp中的命令行参数。/a.out user1.txt(文本文件名可以更改) 我的main.cpp中有以下内容 int main(int argc, char* argv[]){ string name; name = argv[1]; } 但我不知道如何在BBoard cpp中将名称输入我的BBoard设置函数 #include "BBoard.h" #include <fstream> #include <algorit

对于我的作业,它说我要使用main.cpp中的命令行参数。/a.out user1.txt(文本文件名可以更改)

我的main.cpp中有以下内容

int main(int argc, char* argv[]){
    string name;
    name = argv[1];
}
但我不知道如何在BBoard cpp中将名称输入我的BBoard设置函数

#include "BBoard.h"
#include <fstream>
#include <algorithm>
using namespace std;

User user_l;
BBoard::BBoard(){
    title = "Default BBoard";
    vector<User> user_list;
    User current_user;
    vector<Message> message_list;
}

BBoard::BBoard(const string &ttl){
    title = ttl;
}

void BBoard::setup(const string &input_file){
    ifstream fin;;
    fin.open(input_file);
    while(!fin.eof()){
        user_list.push_back(user_l);
    }
}
#包括“BBoard.h”
#包括
#包括
使用名称空间std;
用户;
BBoard::BBoard(){
title=“默认BBoard”;
向量用户列表;
用户当前用户;
向量消息列表;
}
BBoard::BBoard(常量字符串和ttl){
标题=ttl;
}
void BBoard::设置(常量字符串和输入文件){
流鳍;;
fin.open(输入_文件);
而(!fin.eof()){
用户列表。推回(用户列表);
}
}
这里有BBoard header

#ifndef BBOARD_H
#define BBOARD_H

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class User
{
public:
    User() { }

    User(const std::string& _name, const std::string& _pass)
        : name(_name), pass(_pass)
    { }

    friend bool operator==(const User& lhs, const User& rhs)
    {
        return (lhs.name == rhs.name) &&
               (lhs.pass == rhs.pass);
    }
private:
    std::string name;
    std::string pass;
};
class Message{
};
class BBoard{
private:
    string title;
    vector<User> user_list;
    User current_user;
    vector<Message> message_list;
public:
    BBoard();
    BBoard(const string &ttl);
};

#endif
#如果没有
#定义BBU_H
#包括
#包括
#包括
使用名称空间std;
类用户
{
公众:
用户(){}
用户(const std::string&_name,const std::string&_pass)
:name(_name),pass(_pass)
{ }
友元布尔运算符==(常量用户和lhs、常量用户和rhs)
{
返回(lhs.name==rhs.name)&&
(左侧通行==右侧通行);
}
私人:
std::字符串名;
std::字符串传递;
};
类消息{
};
B类板{
私人:
字符串标题;
向量用户列表;
用户当前用户;
向量消息列表;
公众:
b板();
BBoard(常量字符串和ttl);
};
#恩迪夫

编辑:如何使用主cpp中的对象将名称发送到BBoard函数?当我尝试将主cpp包括到我的董事会cpp中时,我会出错。

你太接近了。只需编辑
main()
函数即可创建
BBoard
对象,并将
name
传递给它,传递方式与将
argv[1]
传递给
std::string
相同。然后可以调用该对象上的函数,或者将该对象传递给其他函数


风格建议:

如果有人忘记将文件名传递给程序,该怎么办?就目前而言,你崩溃了。如果
argc
仅为1:

if (argc == 1) {
    cout << "usage: a.out file.txt\n";
    return 1;
}
if(argc==1){

cout创建
BBoard
然后调用
setup
函数怎么样:

if (argc > 1) {
   string name = argv[1];
   BBoard board;
   board.setup(name);
}

当删除标题中的使用名称空间时,他仍然需要在向量前面加上std。我按照建议进行了编辑,但学校可测试文件说,我的BBoard设置函数在fin.open(input_file)中无效;造成了问题。我应该在其中添加常量还是其他什么?我不确定现在要编辑什么:(它给出的错误消息是/tmp/testables/BBoard.cpp:In成员函数'void BBoard::setup(const std::string&'):/tmp/testables/BBoard.cpp:20:错误:调用'std::basic_ifstream>::open(const std::basic_string,std::allocator>&)'/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../../../../../include/c++/4.4.7/fstream:525:注意:候选项为:void std::basic_ifstream::open(const char*,std::ios_base::openmode)[with _CharT=CharT,_Traits=std::char@RikoKurahashi奇怪的是,在C++11之前,IOStreams对象只接受
char*
参数,而不是
std::string
参数。
fin.open(name.C_str())
应该可以正常工作。就像在创建
fin
时指定文件名一样:
fstream-fin(name.C_str())在C++11中,您可以使用
std::string
(即,您可以删除
.C_str()
),但要做到这一点,您可能必须告诉编译器接受C++11。使用GCC或Clang时,您可以使用
std=C++11
g++main.cpp-std=C++11
(旧版本的GCC需要
std=C++0x
)。该消息表示您在使用不接受C++11的编译器时正在将
std::string
传递给
open
。您需要传递
char*
(通过调用
C_str()
())或告诉编译器接受C++11。另一个提示:永远不要包含源文件;即永远不要执行
#包含“main.cpp”
或任何类似内容。