constructors problems

hey,
I've been trying to compile this code for a while but the shell says :



"src/CyberWorm.cpp: In constructor ‘CyberWorm::CyberWorm()’:
src/CyberWorm.cpp:19:2: error: uninitialized member ‘CyberWorm::cyber_worm_dormancy_time_’ with ‘const’ type ‘const int’ [-fpermissive]
CyberWorm::CyberWorm(){}
^
src/CyberWorm.cpp: In copy constructor ‘CyberWorm::CyberWorm(const CyberWorm&)’:
src/CyberWorm.cpp:23:34: error: passing ‘const CyberWorm’ as ‘this’ argument of ‘const string CyberWorm::getWormOs()’ discards qualifiers [-fpermissive]
cyber_worm_os_(other.getWormOs()),
^
src/CyberWorm.cpp:24:38: error: passing ‘const CyberWorm’ as ‘this’ argument of ‘const string CyberWorm::getWormName()’ discards qualifiers [-fpermissive]
cyber_worm_name_(other.getWormName()),
^
src/CyberWorm.cpp:25:46: error: passing ‘const CyberWorm’ as ‘this’ argument of ‘int CyberWorm::getDormant()’ discards qualifiers [-fpermissive]
cyber_worm_dormancy_time_(other.getDormant())"








here is the header file code:


#ifndef CYBER_WORM
#define CYBER_WORM
#include <iostream>
#include <string>
#include <vector>


class CyberWorm
{
private:
const std::string cyber_worm_os_;
const std::string cyber_worm_name_;
const int cyber_worm_dormancy_time_;
CyberWorm(); // Prevent the use of an empty constructor
// Add your variables here

public:
CyberWorm(std::string cyber_worm_os, std::string cyber_worm_name, int cyber_worm_dormancy_time);
CyberWorm(const CyberWorm& other); //copy constructor;
int getDormant();
const std::string getWormName();
const std::string getWormOs();

};


#endif






here is the cpp file code:



#include "../include/CyberWorm.h"

CyberWorm::CyberWorm(std::string cyber_worm_os, std::string cyber_worm_name,int cyber_worm_dormancy_time):
cyber_worm_os_(cyber_worm_os),
cyber_worm_name_(cyber_worm_name),
cyber_worm_dormancy_time_(cyber_worm_dormancy_time)

{
}


CyberWorm::CyberWorm(){}


CyberWorm::CyberWorm(const CyberWorm& other):
cyber_worm_os_(other.getWormOs()),
cyber_worm_name_(other.getWormName()),
cyber_worm_dormancy_time_(other.getDormant())

{
}


int CyberWorm:: getDormant(){
return cyber_worm_dormancy_time_;
}


const std::string CyberWorm::getWormName(){
return cyber_worm_name_;
}

const std::string CyberWorm::getWormOs(){
return cyber_worm_os_;
}


I tried to change the copy constructor to an not initiallion list format but it did'nt help.
Also- in an other cpp file on the same src file I wrote an empty constructor and it does compile.



> error: uninitialized member (...) with ‘const’ type
you have a constant variable but you are not initializating it.
1
2
3
CyberWorm::CyberWorm():
   cyber_worm_dormancy_time_(42)
{}



> error: passing ‘const CyberWorm’ as ‘this’ argument of (...) discards qualifiers
look for const-correctness
Because a constant variable cannot change its state you cannot call methods on it that may change its state.
If you've got a member function that does not change the state of the object you need to inform the compiler, then you can use constant objects with it.
const std::string getWormOs() const;
thanks alot
it wokrs now
Topic archived. No new replies allowed.