Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
See more:
  #ifndef ARRAY_H
  #define ARRAY_H
  #include <iostream>
  using std:: ostream;
  using std::istream;
  int main()
  class array
{
   friend ostream &operator << (ostream &, const Array &);
   friend istream &operator >> (istream &, Array &);
   public:
   Array (int = 10);
   Array (const Array &);
  ~Array();
  int getSize() const;
  const Array &operator =(const Array &);
  bool operator ==(const Array &) const;
  bool operator!=(const Array &right) const
{
  return! (*this == right);
}
  int &operator[]( int );
  int operator[]( int ) const;
  private:
  int size;
  int *ptr;

};
 #endif


What I have tried:

whats wrong?
how to fix error?
error: expected initializer before 'class'
Posted
Updated 12-Jan-20 20:57pm
v2
Comments
0x01AA 11-Jan-20 11:01am
   
"whats wrong?": What I can see, more or less everything is wrong here. I suggest to start with a very simple "Hello World" program to get the basics. Something like this and many more: C++ Hello World with Classes | Studio Freya[^]

Rate this:
Please Sign up or sign in to vote.

Solution 1

There are too many things wrong in the code you show. I suggest to work through a very basci c++ "Hello World" example to learn the basics.

E.g: C++ Hello World with Classes | Studio Freya[^]
   
Comments
Maciej Los 11-Jan-20 13:24pm
   
5ed!
0x01AA 11-Jan-20 13:45pm
   
Thank you Maciej
Rate this:
Please Sign up or sign in to vote.

Solution 3

To add to what has been said - and 0x01AA is spot on here - you are missing a semicolon before class.
Change this:
int main()
To this:
int main();
Or this:
int main(){}
That'll fix the one error you have spotted.
There are more, loads of more...
   
Comments
Stefan_Lang 13-Jan-20 3:01am
   
I don't think fixing compiler errors will lead anywhere in this case. This looks more like a case of not knowing how to use code from other source files in C/C++. In the sense of including code rather than #includeing
Rate this:
Please Sign up or sign in to vote.

Solution 4

Since this is a header file of your own, the main declaration does not need to be there at all. Also, it is helpful to have proper indentation. Like this:
class Array
{
public:
    Array( int = 10 );
    Array( const Array & );
    ~Array();

    const Array &operator = ( const Array & );
    bool operator == ( const Array & ) const;
    bool operator != ( const Array &right ) const   { return ! ( *this == right ); }
    int & operator [] ( int );
    int operator [] ( int ) const;

    friend ostream & operator << ( ostream &, const Array & );
    friend istream & operator >> ( istream &, Array & );

    int getSize() const;

private:
    int   size;
    int * ptr;
};
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

 class array
{
   friend ostream &operator << (ostream &, const Array &);


type names are case sensitive so your "Array" should be "array" as class declaration.

Also since you stripping the std:: namespace your class has a potential collision with the std::array STL type
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

This looks like you'ce copied fragments of code from two or more sources, without knowing what either does.

In the case of your code, it looks like you tried to incorporated code defining the class Array into your main program. However the way you did it is not how you combine code in C or C++! While I could point out what exactly you did wrong, I instead suggest you read this guide: Headers and Includes: Why and How - C++ Forum[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100