initializing arrays
| MBattiston (15) | |||
| when ever i try to initialize this array this way it gives an error char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; this is the error i get error C2059: syntax error : '{' i cant think for the life of me why i get this can anyone please help? | |||
| LacViet (80) | |||
| It run fine for me. But I think the problem is that your array size is small. Try make it 27? | |||
| Scipio (288) | |||
| It runs fine by me to, with the size 26. The problem must be somewhere else, can you upload a bit more of the code? | |||
| CheesyBeefy (80) | |||
| Perhaps the error lies on the line before this? Post your entire code, then we might be able to see what the problem is. | |||
| MBattiston (15) | |||
#include "pTree.h" class Main { public: void createMine(int i); string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }; int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}}; private: }; it wont let me compile! i hope someone can help | |||
| Scipio (288) | |||
Wy are you using type string? I would recommend to use type char[], however, both types works fine on my compiler. Try do compile this code:
If it does compile, the problem is somewhere else in your code. If it dont... Wich compiler do you use? | |||
| MBattiston (15) | |||
| ok when i try to implement the class through Main.cpp which consists of the following, i get that error i am using Microsoft visual c++ 6.0 this is Main.cpp #include "Main.h" void Main::createMine(int i) {/* pNode("Mine " + alphabet[i], production_hours[0][i], production_hours[1][i]); if(node.getName() == "Mine b") { node.setRight(createMine(i+3); } else if(node.getName() == "Mine e") { node.setLeft(createMine(i+3); node.setRight(createMine(i+4); } else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i") { //stop } else { node.setLeft(createMine(i+2)); node.setRight(createMine(i+3)); } */ } | |||
| Umz (153) | |||
For your array, when you declare it with the values you want it to hold you don't need to declare the size of array; the computer counts the variables.
char alphabet[] = {'a','b','c','d'}; | |||
| MBattiston (15) | |||
| yeah thats true, but ive tried to getting rid of the 26 and it still gives the same error i think i might try to use cygwin instead of microsoft visual c++ 6 or is that a bad idea? | |||
| guestgulkan (481) | |||
| Changing your compiler isn't the answer to the problem. I suggest (as has already been suggested) that you post the whole code as it stands, not bits and peices of it which could be taken out of context. | |||
| MBattiston (15) | |||
| i did post it all. just not in one thing ill do it again This file is Main.h #include "pTree.h" class Main { public: void createMine(int i); string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }; int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}}; private: }; this file is Main.cpp #include "Main.h" void Main::createMine(int i) {/* pNode("Mine " + alphabet[i], production_hours[0][i], production_hours[1][i]); if(node.getName() == "Mine b") { node.setRight(createMine(i+3); } else if(node.getName() == "Mine e") { node.setLeft(createMine(i+3); node.setRight(createMine(i+4); } else if(node.getName() == "Mine c" || node.getName() == "Mine h" ||node.getName() == "Mine g"|| node.getName() == "Mine f" || node.getName() == "Mine i") { //stop } else { node.setLeft(createMine(i+2)); node.setRight(createMine(i+3)); }*/ } thats all my code for this class. sorry its not indented and stuff i dont know how to post it so it looks like code any ideas as to why it i get the error: error C2059: syntax error : '{' in regards to string alphabet[26] = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }; | |||
| Scipio (288) | |||
Like i have told you before, the alpabet[26] don't cause an error when i use it. However, the syntax
int var[x][y]={{...}{...}} , as you use in the line after declaring alphabet, does cause an error when i try to compile it. It looks like the same error with another compiler: it says i cant use "{" (->the second "{") before "}" token. Could that be the problem? | |||
| Umz (153) | |||||
| The string class operator[] doesn't do what you think, its not storing an array of strings like you hope; there is no operator for that. You need to store an array of characters not strings: The string operator[] searches through the current string object e.g.
Go back to your original code using the char array and force that to work. If you cant make your original design work change it a bit Ex:
| |||||
| MBattiston (15) | |||
| cheers, ill see if this works and also, how would you create an Array of strings? | |||
| Scipio (288) | |||
| @Umz The following code, using a array of type string, works fine by me:
| |||
| MBattiston (15) | |||
| ok, so the alphabet thing is sorted. I still get a problem with the same error for the other array though ill show you my Main.h file #include "pTree.h" #include <string> using namespace std; class Main { public: Main(); void createMine(int i); string alpha; int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}}; private: }; now my Main.cpp file #include "Main.h" Main::Main() { alpha = "abcdefghijklmnopqrstuvwxyz"; } void Main::createMine(int i) { } so yeah, i have the same issue with the following array as i did with the alphabet array int production_hours[2][9] = {{100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}}; cheers to any one that can help | |||
| Scipio (288) | |||
have you even read my comment:
| |||
| MBattiston (15) | |||
| sorry i missed your comment about the alphabet array i dont know why that doesnt work. its being a lame ass any way ill try this and see if it works int production_hours[2][9] = {100, 200, 50, 25, 70, 120, 80, 60, 50}, {10 , 15 , 8 , 2 , 10, 14 , 11, 4 , 9}; Nope i got the same error is that what u meant for me to do? | |||
| Scipio (288) | |||
| Try to use the #format if you use code in your message, thats easier to read. I'm not very familiar with multideminsinal arrays. After trying i found the following code. This code does compile, and it works as it has to work. However, i cant find the differnce between this and your first code... :)
| |||
| MBattiston (15) | |||
| man this is laaaame hahah i wish mine would just work magically umm thanks for the help scipio ill try to initialize the array as two different arrays and in a different way if you somehow stumble accross my problem and find a way to fix it let me know cheers | |||
Pages: 1 2
This topic is archived - New replies not allowed.
