c++uniform-initialization

C++ uniform declaration is not working in class


I am trying to have a member array in a class with its length specified by the const static int variable for future needs.

My compiler throws an error with it, and I am not sure this is an error about a uniform initialization or array initialization, or both.

Here is the header file:

#ifndef SOUECE_H
#define SOURCE_H

class Test
{
public:
    Test();
    static const int array_length{2};
    double array[array_length];
};

#endif

This is the source file.

#include "source.h"

Test::Test()
    :array_length{0} //Typo of array{0}
{
}

These are the problem messages.
[enter image description here][1] [1]: https://i.sstatic.net/IQ2Mk.png

This is the CMake file.

cmake_minimum_required(VERSION 3.0.0)
project(temp VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(temp main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

Finally, these are what the compiler is complaining about when I try to build the project.

[main] Building folder: temp 
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/USERNAME/Desktop/temp/build --config Debug --target all -j 14 --
[build] [1/2  50% :: 0.066] Building CXX object CMakeFiles/temp.dir/main.cpp.o
[build] FAILED: CMakeFiles/temp.dir/main.cpp.o 
[build] /usr/bin/clang++   -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -MD -MT CMakeFiles/temp.dir/main.cpp.o -MF CMakeFiles/temp.dir/main.cpp.o.d -o CMakeFiles/temp.dir/main.cpp.o -c ../main.cpp
[build] In file included from ../main.cpp:1:
[build] ../source.h:8:22: error: function definition does not declare parameters
[build]     static const int array_length{2};
[build]                      ^
[build] ../source.h:9:18: error: use of undeclared identifier 'array_length'
[build]     double array[array_length];
[build]                  ^
[build] 2 errors generated.
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1

I am using macOS 12.0 beta, with VS Code 1.60.2, clang 13.0.0, CMake 3.20.2.

Please let me know if you see any wrong or have any suggestions.


Solution

  • Since array_length is static const you have to initialize it like this:

    source.h

    #ifndef SOUECE_H
    #define SOURCE_H
    
    class Test
    {
    public:
        Test();
        static const int array_length = 2;
        //static const int array_length{2}; this will also work just note that we cannot use constructor initializer list to initialize static data members
        double array[array_length];
    };
    
    #endif
    
    

    source.cpp

    #include "source.h"
    
    Test::Test()//we cannot use constructor initializer list to initialize array_length
        
    {
    }
    
    

    Note that static const int array_length{2}; will also work but the thing is that in the constructor we cannot use constructor initializer list to initialize static data member.

    EDIT: If you decide to use static const int array_length{2}; then make sure that you have C++11(or later) enabled in your CMakeLists.txt. For this you can add

    set (CMAKE_CXX_STANDARD 11)
    

    to your CMakeLists.txt.

    Alternatively, you could instead add

    target_compile_features(targetname PUBLIC cxx_std_11)
    

    in your CMakeLists.txt. In your case replace targetname with temp since that is the targetname that you have. Check this out for more how to active C++11 in CMake .