Invalid use of class error

Hi, when i compile the code for proj07.driver.cpp, I recieve the following error:

proj07.driver.cpp:10: error: invalid use of 'class String'

I'm not sure how 'class String' is being used incorrectly, any suggestions?

*****************************************************************

//project07.string.h

class String
{
private:
// Capacity of a string
//
static const unsigned int MAX = 64;

char Mem[MAX]; // Memory to hold characters in string
unsigned Len; // Number of characters in string

public:

unsigned length() const { return Len; }
unsigned maximum() const { return MAX; }

// Construct string by copying array of characters
//
String( const char [] );
};

*****************************************************************
//proj07.string.cpp

#include <iomanip>
#include <iostream>
#include "project07.string.h"

using namespace std;

String::String( const char Array[])
{
int SizeOfArray;
int i;
SizeOfArray = sizeof(Array)/sizeof(Array[0]);
for (i = 0; i < SizeOfArray; i++)
{
Mem[i] = Array[i];
}
}

*********************************************************************
//proj07.driver.cpp

#include <iostream>
#include "project07.string.h"

using namespace std;

int main()
{
String One;
String Two;
char Monkeys[] = "Monkeys";
One.String(Monkeys);
for (int i = 0; i < One.length(); i++)
{
cout << One[i] << endl;
}
}







Last edited on
Lost ";" at end of class String
whoops, I forgot to copy the ";" to my post.

I still need suggestions on how 'class String' is being used incorrectly. Anyone?
You need to do something with operator=
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int main()
{
String One;
String Two;
char Monkeys[] = "Monkeys";
One.String(Monkeys);
for (int i = 0; i < One.length(); i++)
{
cout << One[i] << endl;
}
}


Mmm...
error C2512: 'String' : no appropriate default constructor available
error C2512: 'String' : no appropriate default constructor available
error C2274: 'function-style cast' : illegal as right side of '.' operator
warning C4018: '<' : signed/unsigned mismatch
error C2676: binary '[' : 'String' does not define this operator or a conversion to a type acceptable to the predefined operator

Add a construct function with no parameter;
Can't call the cosntruct like line 6;
Should reload operator [];
My code like below, there's a problem: How to reload the operator "<<"? We need change the basic_ostream ? Anyone help me?

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
78
79
80
81
82
83
84
//project07.string.h

class String
{
    // Capacity of a string
    //
    static const unsigned int MAX = 64;

    char Mem[MAX]; // Memory to hold characters in string
    unsigned Len; // Number of characters in string

public:

    unsigned length() const { return Len; }
    unsigned maximum() const { return MAX; }

    // Construct string by copying array of characters
    //
    String( const char [] );
    String();
    String &operator=(const char Array[]);
};

*****************************************************************
//proj07.string.cpp


String::String( const char Array[])
{ 
    //The Size of Array can't get by such code, Array is a pointer
    //int SizeOfArray;
    //int i;
    //SizeOfArray = sizeof(Array)/sizeof(Array[0]);
    //for (i = 0; i < SizeOfArray; i++)
    //{
    //    Mem[i] = Array[i];
    //}
    if (strlen(Array) < MAX)
    {
        memcpy(Mem, Array, strlen(Array)+1);
        Len = strlen(Array);
    }
}

String::String(void)
{
    Len = 0;
    Mem[0] = '\0';
}

String &String::operator=(const char Array[])
{
    if (strlen(Array) < MAX)
    {
        memcpy(Mem, Array, strlen(Array)+1);
        Len = strlen(Array);
    }

    return *this;
}

String &String::operator<<(const char Array[])
{
    if (strlen(Array) < MAX)
    {
        memcpy(Mem, Array, strlen(Array)+1);
        Len = strlen(Array);
    }

    return *this;
}
int main()
{
    String One;
    String Two;
    char Monkeys[] = "Monkeys";
    //One.String(Monkeys);
    One = Monkeys;
    //for (int i = 0; i < One.length(); i++)
    //{
    //    cout << One[i] << endl;
    //}
    cout << One << endl;
}
 
friend std::ostream& operator<<( std::ostream&, const String& );


inside the String class declaration.
Topic archived. No new replies allowed.