向量<;字符串>;c+中的声明+; 我对C++非常陌生,所以很抱歉,如果这个解决方案很琐碎的话。我正在这个网站上搜索解决方案,但没有找到什么。 我需要做一些热力学微积分,因此我要用一些信息来初始化我的课程。由于我的组件的原因,在编译时此代码会中断。 我正在寻找一种将向量传递给我的类的方法,或者另一种在类中为我的微积分提供我想要模拟的混合物的信息的方法

向量<;字符串>;c+中的声明+; 我对C++非常陌生,所以很抱歉,如果这个解决方案很琐碎的话。我正在这个网站上搜索解决方案,但没有找到什么。 我需要做一些热力学微积分,因此我要用一些信息来初始化我的课程。由于我的组件的原因,在编译时此代码会中断。 我正在寻找一种将向量传递给我的类的方法,或者另一种在类中为我的微积分提供我想要模拟的混合物的信息的方法,c++,class,vector,methods,C++,Class,Vector,Methods,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;

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
\ifndef BASE\u H
#定义基准
#包括
#包括
#包括
使用名称空间std;
阶级基础
{
公众:
向量分量_i;
int-nComp,nPhase;
长双T,P,N_P_i,x_P_i;
基(向量,整数,整数,长双精度,长双精度,长双精度);
};
#恩迪夫
作为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;
}
#包括“Base.h”
Base::Base(向量分量i,int-nComp,int-nPhase,长双T,长双P,长双N\u P\i){
碱::组分_i=组分_i//如H2O和乙醇
Base::nComp=nComp;
基::n相=n相;
碱基:T=T;
碱基:P=P;
Base::N_p_i=N_p_i;
}
这根本不起作用,但我想它更清楚地表明了我想做什么


有什么建议吗?

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

vector<string> Base::components_i;
vectorbase::components\u 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());
}
#包括
#包括
#包括
#包括
使用名称空间std;
阶级基础
{
公众:
静态矢量分量;
int-nComp,nPhase;
长双T,P,N_P_i,x_P_i;
基(向量,整数,整数,长双精度,长双精度,长双精度);
};
向量基::分量_i;
Base::Base(向量分量i,int-nComp,int-nPhase,长双T,长双P,长双N\u P\i){
Base::components_i=components_i;//例如H2O和乙醇
Base::nComp=nComp;
基::n相=n相;
碱基:T=T;
碱基:P=P;
Base::N_p_i=N_p_i;
};
int main(){
向量向量机;
向量推回(字符串(“hello”);
基础=基础(vec,1,1,2.0,2.0,3.0);
printf(“%s”,Base::components_i[0].c_str());
}
以下是一个工作示例:


静态成员变量的定义需要在类之外,而不是在成员函数内部。是否确实希望
组件i
静态的
?是否确实需要所有成员变量都是
公共的
?哦,您不需要在成员函数中为它们加前缀
Base::
在头文件中,因为它会传播到包含它的每个文件中。不知道您想要实现的是什么,但它不起作用。我是说,你的语法很不寻常。我会在你的构造函数中做一个:1。初始值设定项列表,
MyClass(int_a,int_b):a(_a),b(_b){…}
对于两个成员a和b,2。命名稍有不同的项的赋值,
MyClass(int\u a,int\u b){a=\u a;b=\u b;}
,3。使用this指针,
MyClass(inta,intb){this->a=a;this->b=b;}
-这些将是常用的语法。也就是说,请用实际的句子陈述你的问题。“为了实现……我想……而我现在的代码……产生了这样的错误:……”考虑到
components\u I
是在构造函数中设置的,它可能实际上不应该是静态的。