繁体   English   中英

向量 <string> C ++中的声明

[英]vector<string> declaration in c++

我是C ++的新手,如果此解决方案非常简单,对不起。 我正在本网站上搜索解决方案,但没有找到任何东西。 我需要做一些热力学演算,并因此从课堂上开始一些信息。 由于我的components_i此代码在编译时会中断。 我正在寻找一种将向量传递给班级的方法,或者寻找一种向班级微积分提供有关我要模拟的混合物的信息的方法。

h文件:

#ifndef BASE_H
#define BASE_H
#include <string> 
#include <vector>
#include <iostream>
using namespace std;

class Base
{
    public:
    vector<string> components_i;
    int nComp,nPhase;
    long double T,P,N_p_i,x_p_i;
    Base( vector<string>,int,int,long double,long double,long double );
};

#endif

并作为cpp:

#include "Base.h"

Base::Base( vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
    Base::components_i = components_i //e.g. H2O and Ethanol
    Base::nComp = nComp;
    Base::nPhase = nPhase;
    Base::T = T;
    Base::P = P;
    Base::N_p_i = N_p_i;
}

这根本不起作用,但我认为这更清楚了我想做什么

有什么建议么?

您没有声明您的静态成员。 需要在类声明之外显式声明静态成员,添加以下内容:

vector<string> Base::components_i;

完整的代码应类似于:

#include <stdio.h>
#include <string> 
#include <vector>
#include <iostream>
using namespace std;

class Base
{
    public:
        static vector<string> components_i;
        int nComp,nPhase;
        long double T,P,N_p_i,x_p_i;
        Base( vector<string>,int,int,long double,long double,long double );
};

vector<string> Base::components_i;

Base::Base(vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
    Base::components_i = components_i; //e.g. H2O and Ethanol
    Base::nComp = nComp;
    Base::nPhase = nPhase;
    Base::T = T;
    Base::P = P;
    Base::N_p_i = N_p_i;
};

int main() {
    vector<string> vec;
    vec.push_back(string("hello"));
    Base base = Base(vec, 1, 1, 2.0, 2.0, 3.0);
    printf("%s", Base::components_i[0].c_str());
}

这是一个工作示例:

http://cpp.sh/9xkfk

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM