-2

Hi Its my first time creating header file and accessing its variables in cpp. I have 2 files arrayyy.h and array.cpp and their code is mentioned below.

arrayyy.h

#ifndef ARRAYYY_H_
#define ARRAYYY_H_

namespace a {

class arrayyy {
public:
    arrayyy();
    virtual ~arrayyy();
private:
    const static int num = 4;
    int* arr = new int[num];
};

} /* namespace a */

#endif /* ARRAYYY_H_ */

arrayyy.cpp

#include "arrayyy.h"
#include "iostream"
using namespace std;

namespace a {

arrayyy::arrayyy() {


    cout << a::arrayyy::num ;

    a::arrayyy::arr = {8,2,9,4};

    for(int i =0; i < a::arrayyy::num; i++)
    {
        cout << arr;
    }
}

arrayyy::~arrayyy() {

}
//int main ()
//{
//  cout << a::arrayyy::num ;
//}

} /* namespace a */

Instead of getting output CTD build console is showing following warnings

12:28:10 **** Incremental Build of configuration Debug for project TRiES ****
make all 
Building file: ../src/arrayyy.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/arrayyy.d" -MT"src/arrayyy.o" -o "src/arrayyy.o" "../src/arrayyy.cpp"
In file included from ../src/arrayyy.cpp:8:0:
../src/arrayyy.h:19:24: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
  int* arr = new int[num];
                        ^
../src/arrayyy.cpp: In constructor ‘a::arrayyy::arrayyy()’:
../src/arrayyy.cpp:19:28: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  a::arrayyy::arr = {8,2,9,4};
                            ^
../src/arrayyy.cpp:19:18: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘int*’ in assignment
  a::arrayyy::arr = {8,2,9,4};
                  ^
src/subdir.mk:18: recipe for target 'src/arrayyy.o' failed
make: *** [src/arrayyy.o] Error 1

12:28:10 Build Finished (took 417ms)

I've checked already asked questions but their answers are not helping me in this regard. I've used extern as well. Being a beginner I need guidance in this scenario. Please guide me to remove the warnings. Thank you.

  • 1. You need to initialise `num` in the constructor. 2. Arrays require a size *constant* already at compile time (i. e. `arr[num]` is simply not possible, at least not the way you try). – Aconcagua Apr 18 '18 at 07:20
  • Possible duplicate of [Array\[n\] vs Array\[10\] - Initializing array with variable vs real number](https://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number). The compiler is telling you what is wrong. You cannot use a `non static variable` for an array size. – user1810087 Apr 18 '18 at 07:22
  • @user1810087 Static alone is not sufficient, must be const, too... – Aconcagua Apr 18 '18 at 07:26
  • Thank you all for suggestions. Can you please point out what else am i doing wrong? – Farwa Ansari Apr 18 '18 at 07:44
  • There is no need to use `a::arrayyy::arr`if you are inside your class and inside your namespace. Just `arr`should be work fine. – haggi krey Apr 18 '18 at 07:50
  • Please start class names with a capital letter, e.g. Arrayyy in your case. – YesThatIsMyName Apr 18 '18 at 07:55

1 Answers1

1

You have two problems here. The first one is already solved by the compiler:

../src/arrayyy.h:19:24: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11

int* arr = new int[num];

This feature was added to the language in the C++11 standard. So, to be able to initialize class members in the class declaration, you need to add the option -std=c++11 to the compile options.

The other part is about this line:

a::arrayyy::arr = {8,2,9,4};

This is not something you can do in C++. The member arr is a pointer to some ints. There is no easy way to assign a whole bunch of those ints in one statement. You will have to do this separately:

arr[0] = 8;
arr[1] = 2;
arr[2] = 9;
arr[3] = 4;

You don't have to use the fully qualified name inside the class members. The compiler still understands that you mean the member variable.

Community
  • 1
  • 1
Bo Persson
  • 90,663
  • 31
  • 146
  • 203