views:

201

answers:

3

I have trouble implementing my class. It should be able to initialize from std::string. So I wrote a copy (?) constructor:

CVariable (std::string&, const int p_flags = 0);

I'm trying to make an object of CVariable:

MCXJS::CVariable s_var = (string)"good job";

I'm getting the following error:

F:\Projekty\MCXJS\src\main.cpp|8|error: conversion from 'std::string' to non-scalar type 'MCXJS::CVariable' requested|

How to fix that?

I'M SEARCHING FOR SOLUTION THAT WILL ALLOW SOMETHING EXACTLY AS FOLLOWS:

MCXJS::CVariable s_var = (string)"good job";

Edit: adding (almost) full source code:

cvariable.h

#ifndef CVARIABLE_H
#define CVARIABLE_H

#include <string>
#include <sstream>

namespace MCXJS
{
enum VARTYPE
{
    STRING = 0,
    INT = 1,
    FLOAT = 2
};

class CVariable
{
    public:
    VARTYPE Type () {return m_type;};
    std::string& Value () {return m_value;};
    bool SetType (VARTYPE);

    private:
    const int m_flags;
    VARTYPE m_type;
    std::string m_value;

    // ctors and operators
    public:
    CVariable (const int p_flags = 0);
    CVariable (CVariable&, const int);
    CVariable (std::string const&, const int);
    CVariable (const int&, const int);
    CVariable (const float&, const int);

    CVariable& operator= (const CVariable&);
    CVariable& operator= (const std::string&);
    CVariable& operator= (const int);
    CVariable& operator= (const float);
};
};

#endif // CVARIABLE_H

cvariable.cpp

#include "cvariable.h"

using namespace MCXJS;
using namespace std;

CVariable::CVariable (const int p_flags):
m_flags (p_flags)
{};

CVariable::CVariable (CVariable& p_var, const int p_flags = 0):
m_flags (p_flags),
m_type (p_var.Type()),
m_value (p_var.Value())
{};

CVariable::CVariable (std::string const& p_value, const int p_flags = 0):
m_flags (p_flags),
m_type (STRING),
m_value (p_value)
{};

CVariable::CVariable (const int p_value, const int p_flags = 0):
m_flags (p_flags),
m_type (INT)
{
std::ostringstream buffer;
buffer << p_value;
m_value = buffer.str();
};

CVariable::CVariable (const float p_value, const int p_flags = 0):
m_flags (p_flags),
m_type (FLOAT)
{
std::ostringstream buffer;
buffer << p_value;
m_value = buffer.str();
};

main.cpp

#include "cvariable.h"
#include <iostream>

using namespace std;

int main()
{
MCXJS::CVariable s_var = (string)"good job"; // error
cout << s_var.Value() << '\n';
return 0;
}

Edit: adding enum VARPARAM

Edit: OK, solved above, now I have this:

cvariable.cpp|12|error: passing 'const MCXJS::CVariable' as 'this' argument of 'MCXJS::VARTYPE MCXJS::CVariable::Type()' discards qualifiers|
cvariable.cpp|13|error: passing 'const MCXJS::CVariable' as 'this' argument of 'std::string& MCXJS::CVariable::Value()' discards qualifiers|
+5  A: 

You need to take this by const reference

CVariable (std::string const&, const int p_flags = 0);

It does not make sense to accept a temporary conversion result by a non-const reference. Changes to that parameter will just be lost afterwards. Making it work by having it be a const reference is easy, so Standard C++ just forbids it.

Johannes Schaub - litb
Still have an error.
Xirdus
@Xirdus you need to add all your default arguments in the header, instead of in the .cpp file...
Johannes Schaub - litb
This helps. Thanks. Now I have new errors
Xirdus
Wow the downvotes are just entertaining. If my answer would be just a blatant guess, I would understand there are downvotes without rationales either. But since I didn't just guess but applied reasoning, I'm going to expect the same for downvote, folks.
Johannes Schaub - litb
Did you down-vote the other answers because of that??
Alerty
@Xirdus ultimately I recommend you to read a good C++ book. Understanding how "const" behaves in C++ requires some reading. I heard the book "Accelerated C++" is a good one.
Johannes Schaub - litb
@Alerty no I didn't downvote the other ones because of that. Please check up a simple C++ code `struct A { void operator=(int); A(int) { } }; int main() { A a = 0; }` and notice that everything goes fine. A rational answer will not suggest things that just can't be true. Helping the questioner by guessing at the reason is not going to help him at all. It will just make him believe that `A a = 0;` will invoke the asignment operator, which is a well-spread misunderstanding.
Johannes Schaub - litb
...so what's wrong with my consts?
Xirdus
A: 

Have you omitted to add the definition of the '=' operator overload in your code sample? You need to properly define what happens when you assign a string to your object.

Alerty
nope <filling chars>
Xirdus
A: 

MCXJS::CVariable s_var = (string)"good job";

Is it a typo? Should be MCXJS::CVariable s_var = { 0, STRING, std::string("good job") }; or even better, explicitly - MCXJS::CVariable s_var(std::string("good job"), 0);.

erjot