Jump to content
  • Advertisement
Sign in to follow this  
Stormtrooper

Initialization list

This topic is 3615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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) 
{ }

Share this post


Link to post
Share on other sites
Advertisement


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

Share this post


Link to post
Share on other sites
// 
// 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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!