2012-12-03 105 views
0

我创建了一个程序,用于从文件中读取数据并从中创建对象。然而,它们的值可能会有所不同,因此我创建了一个可以扩展到一定数量的数组。当我尝试通过构造函数传递时遇到了一些麻烦。此外,我不应该使用从我理解会使这容易很多的载体。如何将数组中的值传递给构造函数? C++

    //bunch of code to read in data from the file similar to this below 
     getline (readfile, typea); 

        //code determining what each data type is.   

readMedia >>NSA_Value; 
for (int i=0; i<=NSA_Value; i++){ 
getline(readMedia, supporting_actorsa[i]); //this is the array I'm using that can grow a bit to accommodate. 

Vhs initial = Vhs(typea, a, b, c, supporting_actorsa[10]); //the constructor ive removed most of the other things to make it more readable. 

这是它调用上创建对象

#include "Vhs.cpp" 


Vhs::Vhs (string type, string a1, string b1, string c1, supporting_actorsa1[10]) 
{ 
} 
Vhs::Vhs(void) 
{ 
} 


Vhs::~Vhs(void) 
{ 
} 

cpp文件,这这这是.h文件中

#pragma once 
class Vhs 
{ 

public: 

    Vhs(void); 
    Vhs (string type, string A2, string B2, string C3, string supporting_actorsA3[10]); 
~Vhs(void); 


}; 

所有的变量存在,我只是删除所有声明让事情看起来更干净。 感谢您的帮助

+1

*“当我试图通过我碰到一些麻烦的构造函数来表示通过” * - 定义“麻烦”。细节很重要。 –

+0

为什么你不能使用矢量?这会让整个情况变得微不足道:) – dreamlax

+3

在那里有一个问题,我知道这是一个问题,但是它可能无法解决我的问题。 –

回答

0

你可以通过一个C-阵列像这样:

#include <iostream> 
#include <string> 
using namespace std; 
class Vhs { 
public: 
    Vhs (string type, string A2, string B2, string C3, 
     string *supporting_actorsA3, int n); 
}; 
Vhs::Vhs (string type, string a1, string b1, string c1, 
      string *supporting_actorsa1, int n) { 
    for (int i = 0; i < n; i++) { 
    cout << supporting_actorsa1[i] << endl; 
    } 
} 

int main() 
{ 
    string supporting_actorsa[1024]; 
    int num_actors; 
    num_actors = 2; 
    supporting_actorsa[0] = "1"; 
    supporting_actorsa[1] = "2"; 
    Vhs initial = Vhs("typea", "a", "b", "c", supporting_actorsa, 
     num_actors); 
    return 0; 
} 
+0

谢谢,我会给这个去看看我是否遇到了更多的问题。感谢您及时的回复! – ceeplusplus

+0

我以为他说他不能使用矢量,他意味着他根本不能使用它,而不仅仅是他的构造函数。向量肯定会使它更容易,但。 – dreamlax

+0

是的,我不能使用矢量:/我遗漏了矢量的东西,它编译,但我不是,如果肯定这意味着我会遇到问题后。 – ceeplusplus