开发者

conversion from 'std::string' to non-scalar type requested

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|


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.


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.


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);.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜