Background: I'm writing some simple programs to re-learn some skills I lost by writing a simple program that simply prints it's version and exits. I want to write it so it is as flexible as possible in the future if I decide to use this code for other projects. I have had issues determining how to implement this, and wrote up a specification to help.
Written Specification:
The program version is a value that identifies the capabilities of the software during a particular point in its life cycle. It consists of a MAJOR VERSION, MINOR VERSION, and BUILD NUMBER. The MAJOR VERSION is reserved for implementations of large features, such as the modification of the program's core services, or changes to the API for core services. The MINOR VERSION is reserved for implementations of small features, such as improvements to reliability and security, and corrections to programmatic blunders. The BUILD NUMBER is incremented every time a public development build is created. It is anticipated that the number of builds will remain minimal.
All values in the program version are defined to start at 0, with the exception of the BUILD NUMBER, which is defined to start at 1. It is required that all values in the program version increase with every corresponding degree of change made available to the public. Any decrease is undefined behavior. For now, the version of the program should not change during runtime. Therefore, it is not expected that the program would be able to be modified or updated while any of its components are running. |
Issue: I'm working in the Eclipse IDE using the G++ toolchain. I've implemented the program version as follows:
Program Source:
<workspace>/src/include/versioning/version-info.hpp
1 2 3 4 5 6 7 8 9 10 11 12
|
#ifndef INCLUDE_VERSIONING_VERSION_INFO_HPP_
#define INCLUDE_VERSIONING_VERSION_INFO_HPP_
const struct PROGRAM_VERSION {
unsigned short MAJOR_VERSION = 0;
unsigned short MINOR_VERSION = 0;
unsigned short BUILD_NUMBER = 1;
void printProgramVersion() {
std::cout << MAJOR_VERSION << "." << MINOR_VERSION << "." << BUILD_NUMBER;
return;
}
};
#endif /* INCLUDE_VERSIONING_VERSION_INFO_HPP_ */
|
<workspace>/src/main.cpp
1 2 3 4 5 6 7 8 9
|
#include <iostream>
#include "include/versioning/version-info.hpp"
int main() {
const struct PROGRAM_VERSION ProgramVersion;
std::cout << "MyProgram Version ";
ProgramVersion.printProgramVersion();
std::cout << " is starting.\n" << std::endl;
return (0);
}
|
What way would you implement this?
Console Output:
In file included from ../src/main.cpp:10:0:
../src/include/versioning/version-info.hpp:11:1: error: ‘const’ can only be specified for objects and functions
const struct PROGRAM_VERSION {
^~~~~
../src/main.cpp: In function ‘int main()’:
../src/main.cpp:19:37: error: passing ‘const PROGRAM_VERSION’ as ‘this’ argument discards qualifiers [-fpermissive]
ProgramVersion.printProgramVersion();
^
In file included from ../src/main.cpp:10:0:
../src/include/versioning/version-info.hpp:16:7: note: in call to ‘void PROGRAM_VERSION::printProgramVersion()’
void printProgramVersion() {
^~~~~~~~~~~~~~~~~~~