Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. NON-SCALAR TYPE CONVERSION!?

NON-SCALAR TYPE CONVERSION!?

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminhelpquestionlounge
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jorgmen
    wrote on last edited by
    #1

    Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

    struct Miembro{
    int id;
    int activo;
    int pass;

    Miembro(int a, int b, int c)
    {
    id = a;
    activo = b;
    pass = c;
    }
    };

    //******************************
    //Main is like:

    #include #include #include #include "structs.h"

    using namespace std;

    int main(void)
    {

    cout<< "hola";

    string nombre;
    nombre = "Jorge";
    cout <<" "<< nombre << endl;

    Miembro memb = new Miembro(1, 1, 123);

    // system("pause");
    // return 0;
    }

    F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

    jorgmen

    enhzflepE P J _ 5 Replies Last reply
    0
    • J Jorgmen

      Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

      struct Miembro{
      int id;
      int activo;
      int pass;

      Miembro(int a, int b, int c)
      {
      id = a;
      activo = b;
      pass = c;
      }
      };

      //******************************
      //Main is like:

      #include #include #include #include "structs.h"

      using namespace std;

      int main(void)
      {

      cout<< "hola";

      string nombre;
      nombre = "Jorge";
      cout <<" "<< nombre << endl;

      Miembro memb = new Miembro(1, 1, 123);

      // system("pause");
      // return 0;
      }

      F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

      jorgmen

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      Because you're creating a new Miembro in this way, all you get back is a pointer to it. You then try to assign this pointer to an object of type Miembro. Just change it to this:

      Miembro *memb = new Miembro(1,1,123);

      Make it work. Then do it better - Andrei Straut

      1 Reply Last reply
      0
      • J Jorgmen

        Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

        struct Miembro{
        int id;
        int activo;
        int pass;

        Miembro(int a, int b, int c)
        {
        id = a;
        activo = b;
        pass = c;
        }
        };

        //******************************
        //Main is like:

        #include #include #include #include "structs.h"

        using namespace std;

        int main(void)
        {

        cout<< "hola";

        string nombre;
        nombre = "Jorge";
        cout <<" "<< nombre << endl;

        Miembro memb = new Miembro(1, 1, 123);

        // system("pause");
        // return 0;
        }

        F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

        jorgmen

        P Offline
        P Offline
        pasztorpisti
        wrote on last edited by
        #3

        OR, you can just say

        Miembro memb(1, 1, 123);

        and this will allocate the memb object on the stack. I think this is the recommended way to allocate small objects whenver possible. Objects allocated on the stack are deleted automatically for you when they run out of scope (usually at the end of your function or if-block or whatever). If you allocate an object from the heap then you get a pointer to the heap and you must delete that object via its pointer when you done with it using the delete keyword. By forgetting to delete heap objects your program might continuously 'eat' the memory until you exit the program (this memory waste is called 'memory leak').

        1 Reply Last reply
        0
        • J Jorgmen

          Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

          struct Miembro{
          int id;
          int activo;
          int pass;

          Miembro(int a, int b, int c)
          {
          id = a;
          activo = b;
          pass = c;
          }
          };

          //******************************
          //Main is like:

          #include #include #include #include "structs.h"

          using namespace std;

          int main(void)
          {

          cout<< "hola";

          string nombre;
          nombre = "Jorge";
          cout <<" "<< nombre << endl;

          Miembro memb = new Miembro(1, 1, 123);

          // system("pause");
          // return 0;
          }

          F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

          jorgmen

          J Offline
          J Offline
          Jorgmen
          wrote on last edited by
          #4

          Thaks for your replays: now i have a new q?

          struct Miembro{
          int id;
          int activo;
          int pass;

          Miembro(int a, int b, int c)
          {
          id = a;
          activo = b;
          pass = c;
          }
          };

          struct NodoMiembro{
          NodoMiembro * nextmiem;
          NodoMiembro * prevmiem;
          Miembro persona;

          NodoMiembro(Miembro nuevo){
          nextmiem = prevmiem = NULL;
          persona = nuevo;
          }
          };

          struct ListaMiembros{
          NodoMiembro * lastmember;
          NodoMiembro * firstmember;

          ListaMiembros(){
          lastmember = firstmember = NULL;

          }

          I can't compile this, I'm having headache because of c++: it says: 4 0 F:\main.cpp In file included from main.cpp F:\structs.h In constructor 'NodoMiembro::NodoMiembro(Miembro)': 31 27 F:\structs.h [Error] no matching function for call to 'Miembro::Miembro()' 31 27 F:\structs.h [Error] candidates are: 17 1 F:\structs.h Miembro::Miembro(int, int, int) 17 1 F:\structs.h candidate expects 3 arguments, 0 provided 12 8 F:\structs.h Miembro::Miembro(const Miembro&) 12 8 F:\structs.h candidate expects 1 argument, 0 provided F:\structs.h At global scope: WHY NodoMiembro can't have a "Miembro xxx" as parameter in the constructor???

          enhzflepE C 2 Replies Last reply
          0
          • J Jorgmen

            Thaks for your replays: now i have a new q?

            struct Miembro{
            int id;
            int activo;
            int pass;

            Miembro(int a, int b, int c)
            {
            id = a;
            activo = b;
            pass = c;
            }
            };

            struct NodoMiembro{
            NodoMiembro * nextmiem;
            NodoMiembro * prevmiem;
            Miembro persona;

            NodoMiembro(Miembro nuevo){
            nextmiem = prevmiem = NULL;
            persona = nuevo;
            }
            };

            struct ListaMiembros{
            NodoMiembro * lastmember;
            NodoMiembro * firstmember;

            ListaMiembros(){
            lastmember = firstmember = NULL;

            }

            I can't compile this, I'm having headache because of c++: it says: 4 0 F:\main.cpp In file included from main.cpp F:\structs.h In constructor 'NodoMiembro::NodoMiembro(Miembro)': 31 27 F:\structs.h [Error] no matching function for call to 'Miembro::Miembro()' 31 27 F:\structs.h [Error] candidates are: 17 1 F:\structs.h Miembro::Miembro(int, int, int) 17 1 F:\structs.h candidate expects 3 arguments, 0 provided 12 8 F:\structs.h Miembro::Miembro(const Miembro&) 12 8 F:\structs.h candidate expects 1 argument, 0 provided F:\structs.h At global scope: WHY NodoMiembro can't have a "Miembro xxx" as parameter in the constructor???

            enhzflepE Offline
            enhzflepE Offline
            enhzflep
            wrote on last edited by
            #5

            Ooo-ouch! Those warning messages are pretty nasty. They offered me no help at all in solving the problem. At first I thought it may be a result of being structs, rather than classes - no, of course not. Default access is public instead of private, but it did then get me thinking. I was writing out the constructors when the idea of default constructors hit me in the face.. Notice in your NodoMiembro definition, you have a variable of type Miembro? Well, have a close look at the way it's defined - It's just Miembro persona; Just back-up a bit and think what would happen if you tried to instantiate the struct like this in your main() - it would fail miserably. Here:

            #include <cstdlib>
            #include <iostream>
            #include <string>

            using namespace std;

            struct Miembro
            {
            int id;
            int activo;
            int pass;

            Miembro(int a, int b, int c)
            {
                id = a;
                activo = b;
                pass = c;
            }
            

            };
            int main(void)
            {
            Miembro a;
            }

            Basically, the fix is to add default constructors to both the the Miembro and the NodoMiembro structs.

            Miembro()
            {
            id=activo=pass=0;
            }

            and

            NodoMiembro()
            {
            nextmiem = prevmiem = NULL;
            }

            Make it work. Then do it better - Andrei Straut

            1 Reply Last reply
            0
            • J Jorgmen

              Thaks for your replays: now i have a new q?

              struct Miembro{
              int id;
              int activo;
              int pass;

              Miembro(int a, int b, int c)
              {
              id = a;
              activo = b;
              pass = c;
              }
              };

              struct NodoMiembro{
              NodoMiembro * nextmiem;
              NodoMiembro * prevmiem;
              Miembro persona;

              NodoMiembro(Miembro nuevo){
              nextmiem = prevmiem = NULL;
              persona = nuevo;
              }
              };

              struct ListaMiembros{
              NodoMiembro * lastmember;
              NodoMiembro * firstmember;

              ListaMiembros(){
              lastmember = firstmember = NULL;

              }

              I can't compile this, I'm having headache because of c++: it says: 4 0 F:\main.cpp In file included from main.cpp F:\structs.h In constructor 'NodoMiembro::NodoMiembro(Miembro)': 31 27 F:\structs.h [Error] no matching function for call to 'Miembro::Miembro()' 31 27 F:\structs.h [Error] candidates are: 17 1 F:\structs.h Miembro::Miembro(int, int, int) 17 1 F:\structs.h candidate expects 3 arguments, 0 provided 12 8 F:\structs.h Miembro::Miembro(const Miembro&) 12 8 F:\structs.h candidate expects 1 argument, 0 provided F:\structs.h At global scope: WHY NodoMiembro can't have a "Miembro xxx" as parameter in the constructor???

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #6

              The declaration

              Miembro persona;

              is the same as saying

              Miembro persona();

              (constructor with 0 arguments) but the only constructor you defined requires 3 arguments

              Miembro(int a, int b, int c)

              so the compiler cannot resolve the reference.

              P 1 Reply Last reply
              0
              • C Chuck OToole

                The declaration

                Miembro persona;

                is the same as saying

                Miembro persona();

                (constructor with 0 arguments) but the only constructor you defined requires 3 arguments

                Miembro(int a, int b, int c)

                so the compiler cannot resolve the reference.

                P Offline
                P Offline
                pasztorpisti
                wrote on last edited by
                #7

                Chuck O'Toole wrote:

                Miembro persona;

                is the same as saying

                Miembro persona();

                Just want to mention an exotic exception: If Miembro was a POD type then the two statements had different effects. The first wouldn't initialize the variable while the second would zero initialize. PODs are primitive types - ints, bools, etc... and structs/classes without any explicitly declared constructors/destructor/virtual methods (briefly: C-struct compatible stuff).

                1 Reply Last reply
                0
                • J Jorgmen

                  Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

                  struct Miembro{
                  int id;
                  int activo;
                  int pass;

                  Miembro(int a, int b, int c)
                  {
                  id = a;
                  activo = b;
                  pass = c;
                  }
                  };

                  //******************************
                  //Main is like:

                  #include #include #include #include "structs.h"

                  using namespace std;

                  int main(void)
                  {

                  cout<< "hola";

                  string nombre;
                  nombre = "Jorge";
                  cout <<" "<< nombre << endl;

                  Miembro memb = new Miembro(1, 1, 123);

                  // system("pause");
                  // return 0;
                  }

                  F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

                  jorgmen

                  P Offline
                  P Offline
                  pasztorpisti
                  wrote on last edited by
                  #8

                  Jorgmen wrote:

                  Sorry you all for bothering on Sunday

                  Those who don't want to be bothered simply don't login on sunday... :-)

                  1 Reply Last reply
                  0
                  • J Jorgmen

                    Sorry you all for bothering on Sunday: I'm new using C++ and I need to simulate a Social network, but I'm having troubles doing a "member": this is the code is very simple: I hava a header called "structs.h"

                    struct Miembro{
                    int id;
                    int activo;
                    int pass;

                    Miembro(int a, int b, int c)
                    {
                    id = a;
                    activo = b;
                    pass = c;
                    }
                    };

                    //******************************
                    //Main is like:

                    #include #include #include #include "structs.h"

                    using namespace std;

                    int main(void)
                    {

                    cout<< "hola";

                    string nombre;
                    nombre = "Jorge";
                    cout <<" "<< nombre << endl;

                    Miembro memb = new Miembro(1, 1, 123);

                    // system("pause");
                    // return 0;
                    }

                    F:\main.cpp In function 'int main()': 17 37 F:\main.cpp [Error] conversion from 'Miembro*' to non-scalar type 'Miembro' requested F:\Makefile.win [Error] [main.o] Error 1 (if this is the only error: please check your library includes)

                    jorgmen

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #9

                    Not a reply to your question, but just for completeness. Always prefer initialization to assignment for member variables. What you've done in the constructor is assignment. Do the following for initialization instead -

                    Miembro(int a, int b, int c) : id(a), activo(b), pass(c)
                    {
                    }

                    «_Superman_»  _I love work. It gives me something to do between weekends.

                    _Microsoft MVP (Visual C++)

                    Polymorphism in C

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups