NationStates Jolt Archive


Who knows C++

I_Hate_Cows
28-02-2005, 03:48
I'm having a fun problem, officially c2664 on Visual C++, I made up a struct something and i managed to make some other stuff work, but I'm getting that error when I try to pass it to a function: cannot convert stupidvariabeltype to stupidvariabletype[][3] (using double arrays and second variable is a constant which equals 3). Any ideas or do you want to see code
EmoBuddy
28-02-2005, 03:49
I'm having a fun problem, officially c2664 on Visual C++, I made up a struct something and i managed to make some other stuff work, but I'm getting that error when I try to pass it to a function: cannot convert stupidvariabeltype to stupidvariabletype[][3] (using double arrays and second variable is a constant which equals 3). Any ideas or do you want to see code
You cannot pass an array to a function - you must pass its reference (or is it its pointer?). That is why I gave up on C++, it's not very friendly. Move to a better language such as Java (I know I'm going to get hated on for saying that, but it's the truth!).

PS post the source and maybe I can show you the specific problem.
I_Hate_Cows
28-02-2005, 03:55
You cannot pass an array to a function - you must pass its reference (or is it its pointer?). That is why I gave up on C++, it's not very friendly. Move to a better language such as Java (I know I'm going to get hated on for saying that, but it's the truth!).

PS post the source and maybe I can show you the specific problem.
Well let me check something, but technically, arrays auto pass by reference.

What I have:

struct ProductType
{
string name;
float price;
};

const int NUMPROCESSORS=5;
const int NUMSOUNDCARDS=3;
const int NUMDRIVES=7;
const int TOTALHARDWARE=3;

void ReadInput(ProductType hardware[NUMPROCESSORS][TOTALHARDWARE]);
int ChooseProcessor();
int ChooseSoundCard();
int ChooseImageDrive();
void CalculatePrice();



int main()
{
ProductType hardware[NUMPROCESSORS][TOTALHARDWARE];

ReadInput(hardware[NUMPROCESSORS][TOTALHARDWARE]);

system("PAUSE");
return (0);
}

void ReadInput(ProductType hardware[NUMPROCESSORS][TOTALHARDWARE])
{
ifstream inFile;
inFile.open("input.txt");
int control1, control2;

if (!inFile)
{
cout << "Error opening file, please try again.";
return;
}

for (control1=0; control1 < TOTALHARDWARE; control1++)
{
for (control2=0; control2 < NUMPROCESSORS; control2++)
{
inFile>>hardware[control2][control1].name>>hardware[control2][control1].price;
}
}

for (control1=0; control1 < TOTALHARDWARE; control1++)
{
for (control2=0; control2 < NUMPROCESSORS; control2++)
{
cout<<hardware[control2][control1].name<<"\t"<<hardware[control2][control1].price<<endl;
}
}

inFile.close();
}
EmoBuddy
28-02-2005, 03:56
Well let me check something, but technically, arrays auto pass by reference.
Hmmm....I seem to remember otherwise, but I haven't programmed in C++ in a few months.
I_Hate_Cows
28-02-2005, 03:57
Hmmm....I seem to remember otherwise, but I haven't programmed in C++ in a few months.
People who don't "know" C++ seem to keep trying to help me. I need some one like if I was doing Spanish, I'd need some one who knew Spanish as in fluent


OK, C has some different rules and stuff than C, and does anyone know know C++ -_-
EmoBuddy
28-02-2005, 04:05
People who don't "know" C++ seem to keep trying to help me. I need some one like if I was doing Spanish, I'd need some one who knew Spanish as in fluent


OK, C has some different rules and stuff than C, and does anyone know know C++ -_-
Well I "know" C++, at least enough such that other people don't feel the need to post. For which line does it give you an error message?
I_Hate_Cows
28-02-2005, 04:07
Well I "know" C++, at least enough such that other people don't feel the need to post. For which line does it give you an error message?
..I get an error when i pass producttype hardward[whatever][whatever] to the function

says: "cannot convert producttype to producttype[][3]"
EmoBuddy
28-02-2005, 04:13
..I get an error when i pass producttype hardward[whatever][whatever] to the function

says: "cannot convert producttype to producttype[][3]"
Try changing this:

ReadInput(hardware[NUMPROCESSORS][TOTALHARDWARE]);

To this:

ReadInput(hardware&);

Or this:

ReadInput(hardware[NUMPROCESSORS][TOTALHARDWARE]&);
Alien Born
28-02-2005, 04:14
I believe that yoiur problem is here

string name;

Strings can not be members of arrays in C++. redefine yourt struct using
char name;

(I last programmed in C++ about 8 years ago, but this is what I get checking in Stroustrup.)
I_Hate_Cows
28-02-2005, 04:16
no worky
Robbopolis
28-02-2005, 04:16
ReadInput(hardware[NUMPROCESSORS][TOTALHARDWARE]);

This line should read: ReadInput(hardware);

Don't put the values in, or it thinks that you're trying to pass it one of the values in the matrix.
I_Hate_Cows
28-02-2005, 04:17
I believe that yoiur problem is here

string name;

Strings can not be members of arrays in C++. redefine yourt struct using
char name;

(I last programmed in C++ about 8 years ago, but this is what I get checking in Stroustrup.)
well besides the fact you can't store more than a char in a char type variable, I managed to get this all to work in the main function, I do not have a problem with the storing, I have a problem with the passing
EmoBuddy
28-02-2005, 04:17
no worky
Then post the whole source file as an attachment (include statements and all). May I'll stick in the compiler and try it out.
EmoBuddy
28-02-2005, 04:18
This line should read: ReadInput(hardware);

Don't put the values in, or it thinks that you're trying to pass it one of the values in the matrix.
yeah exactly (see my above post, you may still need the ampersand).
I_Hate_Cows
28-02-2005, 04:19
This line should read: ReadInput(hardware);

Don't put the values in, or it thinks that you're trying to pass it one of the values in the matrix.
Shit, that works, why didn't I try that. Thanks that was the problem, must make a note.
Robbopolis
28-02-2005, 04:21
Shit, that works, why didn't I try that. Thanks that was the problem, must make a note.

The best part is that I haven't programmed anything in over a year.
Cogitation
28-02-2005, 04:22
People who don't "know" C++ seem to keep trying to help me. I need some one like if I was doing Spanish, I'd need some one who knew Spanish as in fluent

OK, C has some different rules and stuff than C, and does anyone know know C++ -_-
I appreciate the fact that C has some differences from C++ and that, ideally, you need someone who's fluent in C++. That appreciation stated, I will admit that I only know C and not C++.

All of that said, I'm pretty sure your code is wrong and I'll explain why I think that.

First, notice that you've defined NUMPROCESSORS and TOTALHARDWARE as constant integers, such that their values are 5 and 3, respectively. Thus, whenever the program comes across NUMPROCESSORS, it replaces it with 5, and whenever it comes across TOTALHARDWARE, it replaces it with 3. Thus, the function definition and definition both become

void ReadInput(ProductType hardware[5][3])

...meaning that it needs to be passed a 5-by-3 array of variables of type "ProductType". This is fine. The problem is when you try to call the function. Look at your main function code:

int main()
{
ProductType hardware[NUMPROCESSORS][TOTALHARDWARE];

ReadInput(hardware[NUMPROCESSORS][TOTALHARDWARE]);

system("PAUSE");
return (0);
}
That line in bold is being interpreted as

ReadInput(hardware[5][3]);

This means that you are not trying to pass the "hardware" array over to the function, but rather that you're trying to either:
Pass only one element of the array over to the function, or
Pass over a memory pointer that points to the memory address of the [5][3] element of the "hardware" array (which is wrong for reasons that I won't go into right now).

Given the way you declared the "hardware" array in "main()", I suspect that you're accidentally doing the first: trying to pass only one element of the array over to the function "ReadInput".

To correct this problem, you need to change the main function as follows:
int main()
{
ProductType hardware[NUMPROCESSORS][TOTALHARDWARE];

ReadInput(hardware);

system("PAUSE");
return (0);
}

Try that and see if it works. If it does work, then your problem is fixed. If it doesn't work, then you really do need someone fluent specifically in C++ (rather than C), and I offer my apologies for taking up your time.

I see "Robbopolis" has beaten me to the punch. :p

--The Democratic States of Cogitation
"Think about it for a moment."
I_Hate_Cows
28-02-2005, 04:26
Yeah Cog, I appreciate the lengthy explanation, but you got beaten by the shorter answer, though the lengthy explanation is good to know exactly why it was wrong.
Robbopolis
28-02-2005, 04:37
I see "Robbopolis" has beaten me to the punch. :p

Bow before the awesome programming skills of the philosophy major.