C++ Error: conversion to non-scalar type requested?

camohreally

I'm fairly new to C++ and i'm trying to build a linked list with a container class called FlexString. In main() I want to instantiate the FlexString class by simply saying: "FlexString flex_str = new FlexString();" calling the constructor etc. But it won't compile, the error is at the bottom. Here is my code:

    //FlexString.h file
#ifndef FLEXSTRING_CAMERON_H
#define FLEXSTRING_CAMERON_H
#include "LinkedList.h"
#include <string>

using namespace std;
using oreilly_A1::LinkedList;

namespace oreilly_A1 {
class FlexString {
public:

    FlexString();
    void store(std::string& s);
    size_t length();
    bool empty();
    std::string value();
    size_t count();




private:

    LinkedList data_list;

};
}
#endif

Here is the .cpp file for the FlexString class:

#include "FlexString.h"
#include "LinkedList.h"
#include <string>

using namespace std;

namespace oreilly_A1 {
    FlexString::FlexString() {

    }

    void FlexString::store(string& s) {
        data_list.list_head_insert(s);
    }

    std::string value() {
        data_list.list_getstring(); 
    }

}

Here's the main program file.

#include <iostream>
#include <cstdlib>
#include "FlexString.h"

using namespace std;
using oreilly_A1::FlexString;

int main() {

    FlexString flex_str = new FlexString();
    cout << "Please enter a word: " << endl;
    string new_string;
    cin >> new_string;

    flex_str.store(new_string);

    cout << "The word you stored was: "+ flex_str.value() << endl;

}

error: conversion from 'oreilly_A1::FlexString*' to non-scalar type 'oreilly_A1::FlexString' requested. "FlexString flex_str = new FlexString();"

R Sahu
FlexString flex_str = new FlexString();

is wrong since the RHS of the assignment is a pointer to a FlexString while the LHS is an object.

You can use:

// Use the default constructor to construct an object using memory
// from the stack.
FlexString flex_str;

or

// Use the default constructor to construct an object using memory
// from the free store.
FlexString* flex_str = new FlexString();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Error: conversion to non-scalar type requested?

From Dev

c++ Conversion from 'B' to non-scalar type 'A' requested

From Dev

Conversion to non-scalar type requested

From Dev

error: conversion from ‘int’ to non-scalar type ‘BIGNUM {aka bignum_st}’ requested

From Dev

error:conversion from 'QFont' to non-scalar type 'QFontMetricsF' requested-Qt

From Dev

Why does the compilation fail with error: conversion to non-scalar type requested

From Dev

c++ struct operator: conversion from to non-scalar type requested

From Dev

C++ templated cast operator - conversion from to non-scalar type requested

From Dev

conversion from to non-scalar type requested ( functional )

From Dev

conversion to non-scalar type

From Dev

conversion to non-scalar type

From Dev

conversion from ‘std::vector<AdjacencyData> (*)()’ to non-scalar type ‘std::vector<AdjacencyData>’ requested

From Dev

conversion from `const char[2]' to non-scalar type `Persona' requested

From Dev

std::variant, a wrapper class, and 'conversion from ... to non-scalar type ... requested'

From Dev

error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type

From Dev

error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type

From Dev

C How do i cast a scalar type to a non scalar and backwards?

From Dev

When assigning a char array to union datatype, my compiler throws an error: "conversion from char* to non-scalar type"

From Dev

C++ Type conversion error /

From Dev

Conversion from 'int' to non-scalar type assignment operator - object to int

From Dev

Conversion from 'int' to non-scalar type assignment operator - object to int

From Dev

Converting to non-scalar type

From Dev

How do is resolve conversion error from Point<T> to non scalar Point<U>

From Dev

Realloc pollfd - conversion to non-scalar

From Dev

c++ error "Braces around scalar initializer for type int*"

From Dev

C++ incomplete type and conversion error

From Dev

data type conversion error in C#

From Dev

C# type conversion: Explicit cast exists but throws a conversion error?

From Dev

C# type conversion: Explicit cast exists but throws a conversion error?

Related Related

  1. 1

    C++ Error: conversion to non-scalar type requested?

  2. 2

    c++ Conversion from 'B' to non-scalar type 'A' requested

  3. 3

    Conversion to non-scalar type requested

  4. 4

    error: conversion from ‘int’ to non-scalar type ‘BIGNUM {aka bignum_st}’ requested

  5. 5

    error:conversion from 'QFont' to non-scalar type 'QFontMetricsF' requested-Qt

  6. 6

    Why does the compilation fail with error: conversion to non-scalar type requested

  7. 7

    c++ struct operator: conversion from to non-scalar type requested

  8. 8

    C++ templated cast operator - conversion from to non-scalar type requested

  9. 9

    conversion from to non-scalar type requested ( functional )

  10. 10

    conversion to non-scalar type

  11. 11

    conversion to non-scalar type

  12. 12

    conversion from ‘std::vector<AdjacencyData> (*)()’ to non-scalar type ‘std::vector<AdjacencyData>’ requested

  13. 13

    conversion from `const char[2]' to non-scalar type `Persona' requested

  14. 14

    std::variant, a wrapper class, and 'conversion from ... to non-scalar type ... requested'

  15. 15

    error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type

  16. 16

    error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type

  17. 17

    C How do i cast a scalar type to a non scalar and backwards?

  18. 18

    When assigning a char array to union datatype, my compiler throws an error: "conversion from char* to non-scalar type"

  19. 19

    C++ Type conversion error /

  20. 20

    Conversion from 'int' to non-scalar type assignment operator - object to int

  21. 21

    Conversion from 'int' to non-scalar type assignment operator - object to int

  22. 22

    Converting to non-scalar type

  23. 23

    How do is resolve conversion error from Point<T> to non scalar Point<U>

  24. 24

    Realloc pollfd - conversion to non-scalar

  25. 25

    c++ error "Braces around scalar initializer for type int*"

  26. 26

    C++ incomplete type and conversion error

  27. 27

    data type conversion error in C#

  28. 28

    C# type conversion: Explicit cast exists but throws a conversion error?

  29. 29

    C# type conversion: Explicit cast exists but throws a conversion error?

HotTag

Archive