0

I came across to redefinition error when pass string(argv[]) to class constructor as argument. The sample codes are as follows. cA1 produced redefinition error but all cA2, cA3, cA4, and func are fine. Could anyone help me on it?

class A
{
public:
    A(string String1, string String2);
    ~A();

};

void func(string String1, string String2)
{

}

void main(int argc, char** argv)
{
    //error C2086: 'std::string argv[]' : redefinition
    A cA1(string(argv[1]), string(argv[2]));

    string String1 = string(argv[1]);
    string String2 = string(argv[2]);
    //fine 
    A cA2(String1, String2);

    //fine 
    A cA3(argv[1], argv[2]);

    //fine
    A cA4(string(argv[1]).c_str(), string(argv[2]).c_str());

    //fine
    func(string(argv[1]), string(argv[2]));
}
Jackypengyu
  • 251
  • 3
  • 5

1 Answers1

3

A cA1(string(argv[1]), string(argv[2])); doesn't call your constructor, however, it declares a function name cA1'Most vexing parse: why doesn't A a(()); work?', with parameters string(argv[1]) and string(argv[2]) both having same argument variable name argv, hence the error

Community
  • 1
  • 1
P0W
  • 46,614
  • 9
  • 72
  • 119