Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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|
like image 875
Xirdus Avatar asked Apr 18 '26 11:04

Xirdus


1 Answers

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.

like image 51
Johannes Schaub - litb Avatar answered Apr 20 '26 07:04

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!