Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
  #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
[no name] 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[^]

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[^]
 
Share this answer
 
Comments
Maciej Los 11-Jan-20 13:24pm    
5ed!
[no name] 11-Jan-20 13:45pm    
Thank you Maciej
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...
 
Share this answer
 
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
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:
C++
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;
};
 
Share this answer
 
 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
 
Share this answer
 
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900