Initialization list

Started by
7 comments, last by Stormtrooper 15 years, 3 months ago
My initialization list isn't setting my variables. No compiler errors, no run time errors.
Task::Task(const std::string &task,
           const std::string &project = "none",
           const std::string &context = "none",
           Task::Priority priority = Task::NORMAL,
           bool done = false,
           bool nextAction = false) :
     _task(task), _project(project), _context(context), _priority(priority),
     _done(done), _nextAction(nextAction) 
{ }
Advertisement
Post a complete example. That piece of code says nothing, as it is correct by itself.


int main(int argc, char *argv[]){    Task *task = new Task("Hello world");    std::cout << task->getTask() << std::endl;    return 0;    }



// //  task.h//  rtodo2//  //  This code has been released under the BSD license//  See license.txt//  Copyright 2008 Ryan Capote All rights reserved.// #ifndef TASK_H#define TASK_H#include <string>class Task{public:        typedef enum Priority {NORMAL, LOW, MEDIUM, HIGH};        Task(const std::string &task,         const std::string &project,         const std::string &context,         Priority priority,         bool done,         bool nextAction);        Task(const std::string &data);        void loadData(const std::string &data);    std::string serialize();        std::string getTask();    void setTask(const std::string &task);        std::string getProject();    void setProject(const std::string &task);        std::string getContext();    void setContext(const std::string &task);        Priority getPriority();    void setPriority(Priority priority);        bool isDone();    void isDone(bool done);        bool isNextAction();    void isNextAction(bool nextAction);    private:    std::string _task;    std::string _project;    std::string _context;    Priority _priority;    bool _done;    bool _nextAction;};#endif
Implementation of std::string Task::getTask()?
// //  task.h//  rtodo2//  //  This code has been released under the BSD license//  See license.txt//  Copyright 2008 Ryan Capote All rights reserved.//#include "task.h"#include <sstream>Task::Task(const std::string &task,           const std::string &project = "none",           const std::string &context = "none",           Task::Priority priority = Task::NORMAL,           bool done = false,           bool nextAction = false) :     _task(task), _project(project), _context(context), _priority(priority),     _done(done), _nextAction(nextAction) { }Task::Task(const std::string &data){    }void Task::loadData(const std::string &data){    }std::string Task::serialize(){    std::string result;    std::stringstream stream;        stream << _task         << ','           << _project      << ','           << _context      << ','           << _priority     << ','           << _done         << ','           << _nextAction   << '|';                              stream.flush();    result = stream.str();     return result;}std::string Task::getTask(){    return _task;}void Task::setTask(const std::string &task){    _task = task;}std::string Task::getProject(){    return _project;}void Task::setProject(const std::string &project){    _project = project;}std::string Task::getContext(){    return _context;}void Task::setContext(const std::string &context){    _context = context;}Task::Priority Task::getPriority(){    return _priority;}void Task::setPriority(Task::Priority priority){    _priority = priority;}bool Task::isDone(){    return _done;}void Task::isDone(bool done){    _done = done;}bool Task::isNextAction(){    return _nextAction;}void Task::isNextAction(bool nextAction){    _nextAction = nextAction;}
Your code is not complete. Code for Task::getTask() is missing.

edit: ok, way too slow...
Your code isn't correct as it is. You have two constructors taking a single std::string by constant reference as an argument, which results in an ambiguous constructor call.

That makes me believe your compiler is not catching it and calling the empty one in your code, not reporting an error or warning for the other constructor, which in turn makes you believe the initializer list isn't working because it's not called.
Put your default arguments on the declaration, not the definition.
Ah! Okay, I get it. Thanks for your help, I should have know that :P

This topic is closed to new replies.

Advertisement