0

I've got an assignment that I am working on, so I am hesitant of posting all of my code on here (for the off chance that my professor somehow would find it - the policy is that would constitute as cheating).

My issue comes at this though: I've got a visual C++ program that has a textbox that takes a user inputs a string, clicks a button, and adds that string to a vector. I then need to pass that vector (full of strings) into a newly created object via its parameterized constructor. There is also some inheritance involved.

basically, in the .h file for the object and the forms .h file, I have the following (as per how the teacher wanted it to be in the object .h file that I am not allowed to change):

typedef vector<string> StringList;

I then create a new parent object and a new vector in the form's .h file:

private: Trial * t;
private: StringList * strings;

then initialize it in the form's constructor:

strings = new StringList;

I then try to create a new instance of a derived class and assign it on the parent object, while passing the required parameters:

t = new Vector_Trial( strings, multi, type );

I get a red line underneath the first parenthesis that says:

Error: no instance of constructor "Vector_Trial::Vector_Trial" matches the argument list
argument types are:(StringList *, int, Trial::TrialType)

The following are the constructors for the parent class and the derived class:

Trial ( StringList & s, int num_runs, TrialType t )
      : strings(s), numRuns(num_runs), type(t) { }

Vector_Trial ( StringList & s, int num_runs, TrialType t ) : Trial ( s, num_runs, t ) {}

I'm assuming my issue is with the vector.. Any ideas?

Deidrei
  • 2,125
  • 1
  • 14
  • 14
  • Why don't you just declare the private member as "StringList strings" instead, and just ditch the call to `new`? – Joe Z Dec 11 '13 at 02:55
  • Just use `StringList strings`, not using the pointer version, then in the `Vector_Trial` constructor you can call swap method on the input `StringList` to swap the strings in. – Zhi Wang Dec 11 '13 at 03:14
  • Unfortunately, when I do that, I get the error: Error: A member of a managed class cannot be of a non-managed class type – user3089335 Dec 11 '13 at 03:21
  • I appears that you need to create Vector_Trial as an 'unmanaged class' and then wrap in in a 'managed' class via a pointer (pimpl). Personally I would avoid using a .NET compiler which adds this sort of nonsense to C++. See http://stackoverflow.com/questions/11588261/unmanaged-var-as-member-of-managed-class-c – ascotan Jul 10 '16 at 00:53

2 Answers2

0

If you use this function,

Vector_Trial ( StringList & s, int num_runs, TrialType t )

You will pass to it a StringList object, not a pointer to StringList. Should you change your strings declaration as below,

private: StringList strings;
Deidrei
  • 2,125
  • 1
  • 14
  • 14
0

I don't see why you need to make strings a StringList*.

Instead, just declare it as StringList strings;, and remove the call to new.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • When I do that, I get the error: Error: A member of a managed class cannot be of a non-managed class type – user3089335 Dec 11 '13 at 03:04
  • @user3089335 : Ahh, you're programming in C++/CLI? That's a different language... I'm not sure how to best help you. You could try something ugly like passing `*strings` as an argument to `Vector_Trial` as a hack, but... ew? – Joe Z Dec 11 '13 at 03:06