Error: expected constructor, destructor, or type conversion before '=' token
jlmckelvey91 (18)
Dec 3, 2012 at 4:00pm UTC
I keep getting this error, i was up until almost four working on this program. All help would be appreciated. This is the code that gives me the error:
1 2 3 4 5 6 7
struct letterCount
{
char letter;
unsigned int count;
};
letterCount *lc;
lc = new letterCount[num];
Darkmaster (494)
Dec 3, 2012 at 4:07pm UTC
1 2 3 4 5 6 7
struct letterCount
{
char letter;
unsigned int count;
};
int num = 10;
letterCount *lc = new letterCount[num];
Edit: i forgot it's an array :D
you call the elements like this:
1 2 3 4 5
int main()
{
lc[0].count = 100;
}
Last edited on Dec 3, 2012 at 4:17pm UTC
jlmckelvey91 (18)
Dec 3, 2012 at 4:18pm UTC
Ok i tried that, it didn't work, i tried moving it to a function and that made the error stop, however, now it says error: invalid type of argument 'unary *'
these are the functions being called:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
char *createArray(int num)
{
cp = new char [num];
for (int ix = 0; ix < num; ix++)
{
getLowerCaseLetter();
*(cp + ix) = getLowerCaseLetter();
}
return cp;
}
int *countLetters(int num)
{
letterCount *lc;
lc = new letterCount[num];
for (int j = 0; j < num; j++)
{
for (int k = 0; k < num; k++)
{
if (*cp[k] == *lc[j].letter)
{
*lc[j].count++;
}
}
cout << "The letter " << lc[j].letter << " appeared " << lc[j].count << " times.\n" ;
}
}
jlmckelvey91 (18)
Dec 3, 2012 at 4:19pm UTC
and the error is appearing in lines 20 and 22. the first function creates a random char array. the second one counts each time a letter appears.
Last edited on Dec 3, 2012 at 4:19pm UTC
Darkmaster (494)
Dec 3, 2012 at 4:20pm UTC
you cant create arrays during runtime, they need to have a fixed size
just use a vector instead
Last edited on Dec 3, 2012 at 4:22pm UTC
jlmckelvey91 (18)
Dec 3, 2012 at 4:29pm UTC
my professor has not covered vectors yet. i don't think they get covered until the class after the one i'm in. I'm reading up on them now, but could you get a little more specific? also, if it helps, i'll include the entire source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int num;
char *cp;
char *createArray(int );
char getLowerCaseLetter();
int *countLetters(int num);
int displayLetters(char *, int );
int displayCount(char *, int );
struct letterCount
{
char letter;
unsigned int count;
};
int main()
{
srand(time(0));
cout << "Enter an integer to set the array size: " ;
cin >> num;
char * ap;
ap = createArray(num);
displayLetters(ap, num);
displayCount( cp, num);
}
char getLowerCaseLetter()
{
return (char ) ('a' + rand()%25);
}
char *createArray(int num)
{
cp = new char [num];
for (int ix = 0; ix < num; ix++)
{
getLowerCaseLetter();
*(cp + ix) = getLowerCaseLetter();
}
return cp;
}
int *countLetters(int num)
{
letterCount *lc;
lc = new letterCount[num];
for (int j = 0; j < num; j++)
{
for (int k = 0; k < num; k++)
{
if (*cp[k] == *lc[j].letter)
{
*lc[j].count++;
}
}
cout << "The letter " << lc[j].letter << " appeared " << lc[j].count << " times.\n" ;
}
}
int displayLetters(char * , int num)
{
for (int m = 0; m < num; m++)
{
cout << *(cp+m) << " " ;
}
cout << endl;
}
int displayCount(char *, int num)
{
int *mp;
mp = countLetters(num);
}
Last edited on Dec 3, 2012 at 4:31pm UTC
Darkmaster (494)
Dec 3, 2012 at 4:32pm UTC
all your int function have to return a value or make them void
also remove the "*" in line 55 and 57
and change lines 49 and 50 to letterCount *lc = new letterCount[num];
and can you tell me why lc has to be an array?
Last edited on Dec 3, 2012 at 4:37pm UTC
jlmckelvey91 (18)
Dec 3, 2012 at 4:41pm UTC
actually it doesn't. that's tired thinking on my part. and I already tried removing the '*' from the above mentioned lines and the problem with that is that then it just prints out the memory address of each letter as opposed to the letter itself.
jlmckelvey91 (18)
Dec 3, 2012 at 4:43pm UTC
and for whatever reason if lc is not an array, the program compiles then crashes.
edit:
just tried removing the '*' again, and it counted letters but it was giving me letters that weren't part of the array, or repeating random letters, and for the count it just gave me memory locations still.
Last edited on Dec 3, 2012 at 4:45pm UTC
Topic archived. No new replies allowed.