Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Trying to us C++ list's but getting errors

  1. #1
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    7,551

    Trying to us C++ list's but getting errors

    Trying to work a new project that uses c++ "list" but when I do a compile getting some strange errors. So I went and found a nice simple example:
    Code:
    // example taken from:
    //
    //https://en.cppreference.com/w/cpp/container/list
    
    #include <algorithm>
    #include <list>
     
    void setup()
    {
        // Create a list containing integers
        std::list<int> l = { 7, 5, 16, 8 };
     
        // Add an integer to the front of the list
        l.push_front(25);
        // Add an integer to the back of the list
        l.push_back(13);
     
        // Insert an integer before 16 by searching
        auto it = std::find(l.begin(), l.end(), 16);
        if (it != l.end()) {
            l.insert(it, 42);
        }
     
        // Iterate and print values of the list
        for (int n : l) {
            Serial.println( n );
        }
    }
    
    void loop(){}
    but this gives me the following error messages:
    Arduino: 1.8.7 (Windows 10), TD: 1.44-beta2, Board: "Teensy 3.5, Serial, 120 MHz, Faster, US English"

    C:\Users\CYBERP~1\AppData\Local\Temp\arduino_build _993502\sketch\list_example.ino.cpp.o: In function `void std::__cxx11::list<int, std::allocator<int> >::_M_insert<int const&>(std::_List_iterator<int>, int const&)':

    c:\local programs\arduino-1.8.7 arm5\hardware\tools\arm\arm-none-eabi\include\c++\5.4.1\bits/stl_list.h:1764: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__de tail::_List_node_base*)'

    c:\local programs\arduino-1.8.7 arm5\hardware\tools\arm\arm-none-eabi\include\c++\5.4.1\bits/stl_list.h:1764: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__de tail::_List_node_base*)'

    c:\local programs\arduino-1.8.7 arm5\hardware\tools\arm\arm-none-eabi\include\c++\5.4.1\bits/stl_list.h:1764: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__de tail::_List_node_base*)'

    C:\Users\CYBERP~1\AppData\Local\Temp\arduino_build _993502\sketch\list_example.ino.cpp.o: In function `std::_List_iterator<int> std::__cxx11::list<int, std::allocator<int> >::emplace<int>(std::_List_const_iterator<int>, int&&)':

    c:\local programs\arduino-1.8.7 arm5\hardware\tools\arm\arm-none-eabi\include\c++\5.4.1\bits/list.tcc:91: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__de tail::_List_node_base*)'

    collect2.exe: error: ld returned 1 exit status

    Error compiling for board Teensy 3.5.

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.
    What I am really trying to get working is a neural network example but can't get past just setting up the "lists" so I can work the other issues. I know its not the whole code but trying to fix the "list" issue first:

    Code:
    /*Neural Network created by ScottC on 15th Aug 2011
     
    Please visit my blog for a detailed explanation of my Neural Network
    http://arduinobasics.blogspot.com/p/arduinoprojects.html
     
    */
    #include <list>
       
    void setup(){
      std::list<int> myTrainingInputs();
      std::list<int> myTrainingOutputs();
      std::list<int>::iterator itTI;
      std::list<int>::iterator itTO;
      
      itTI = myTrainingInputs.begin();
      itTO = myTrainingOutputs.begin();
      
      float myInputsA[]={0,0};
      float myInputsB[]={0,1};
      float myInputsC[]={1,0};
      float myInputsD[]={1,1};
      float myOutputsA[]={1};
      float myOutputsB[]={0};
       
       
      Serial.println("TRAINING DATA");
      Serial.println("--------------------------------------------");
      
      myTrainingInputs.insert(itTI, myInputsA);
      myTrainingOutputs.insert(itTO,myOutputsA);
      itTI++; itTO++;
      Serial.println("INPUTS= " + myInputsA[0] + ", " + myInputsA[1] + "; Expected output = " + myOutputsA[0]);
      myTrainingInputs.insert(itTI,myInputsB);
      myTrainingOutputs.insert(itTO,myOutputsB);
      itTI++; itTO++;
      Serial.println("INPUTS= " + myInputsB[0] + ", " + myInputsB[1] + "; Expected output = " + myOutputsB[0]);
      myTrainingInputs.insert(itTI,myInputsC);
      myTrainingOutputs.insert(itTO,myOutputsB);
      itTI++; itTO++;
      Serial.println("INPUTS= " + myInputsC[0] + ", " + myInputsC[1] + "; Expected output = " + myOutputsB[0]);
      myTrainingInputs.insert(itTI,myInputsD);
      myTrainingOutputs.insert(itTO,myOutputsA);
      
      Serial.println("INPUTS= " + myInputsD[0] + ", " + myInputsD[1] + "; Expected output = " + myOutputsA[0]);
      Serial.println("--------------------------------------------");
      
      NeuralNetwork NN = new NeuralNetwork();
      NN.addLayer(2,2);
      NN.addLayer(2,1);
      
      Serial.println("Before Training");
      float myInputDataA1[]={0,0};
      NN.processInputsToOutputs(myInputDataA1);
      float myOutputDataA1[]={};
      myOutputDataA1=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 0,0; OUTPUT=" + myOutputDataA1[0]);
       
      float myInputDataB1[]={0,1};
      NN.processInputsToOutputs(myInputDataB1);
      float myOutputDataB1[]={};
      myOutputDataB1=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 0,1; OUTPUT=" + myOutputDataB1[0]);
       
      float myInputDataC1[]={1,0};
      NN.processInputsToOutputs(myInputDataC1);
      float myOutputDataC1[]={};
      myOutputDataC1=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 1,0; OUTPUT=" + myOutputDataC1[0]);
       
      float myInputDataD1[]={1,1};
      NN.processInputsToOutputs(myInputDataD1);
      float myOutputDataD1[]={};
      myOutputDataD1=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 1,1; OUTPUT=" + myOutputDataD1[0]);
     
      Serial.println("");
      Serial.println("--------------------------------------------");
       
      Serial.println("Begin Training");
      NN.autoTrainNetwork(myTrainingInputs,myTrainingOutputs,0.0001,500000);
      Serial.println("");
      Serial.println("End Training");
      Serial.println("");
      Serial.println("--------------------------------------------");
      Serial.println("Test the neural network");
      float myInputDataA2[]={0,0};
      NN.processInputsToOutputs(myInputDataA2);
      float myOutputDataA2[]={};
      myOutputDataA2=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 0,0; OUTPUT=" + myOutputDataA2[0]);
       
      float myInputDataB2[]={0,1};
      NN.processInputsToOutputs(myInputDataB2);
      float myOutputDataB2[]={};
      myOutputDataB2=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 0,1; OUTPUT=" + myOutputDataB2[0]);
       
      float myInputDataC2[]={1,0};
      NN.processInputsToOutputs(myInputDataC2);
      float myOutputDataC2[]={};
      myOutputDataC2=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 1,0; OUTPUT=" + myOutputDataC2[0]);
       
      float myInputDataD2[]={1,1};
      NN.processInputsToOutputs(myInputDataD2);
      float myOutputDataD2[]={};
      myOutputDataD2=NN.getOutputs();
      Serial.println("Feed Forward:  INPUT = 1,1; OUTPUT=" + myOutputDataD2[0]);
      
       
    }
    
    void loop(){}
    but getting the following errors related to "list":
    NN_AB: In function 'void setup()':
    NN_AB:15: error: request for member 'begin' in 'myTrainingInputs', which is of non-class type 'std::__cxx11::list<int>()'
    itTI = myTrainingInputs.begin();

    ^

    NN_AB:16: error: request for member 'begin' in 'myTrainingOutputs', which is of non-class type 'std::__cxx11::list<int>()'
    itTO = myTrainingOutputs.begin();

    ^

    NN_AB:29: error: request for member 'insert' in 'myTrainingInputs', which is of non-class type 'std::__cxx11::list<int>()'
    myTrainingInputs.insert(itTI, myInputsA);

    ^

    NN_AB:30: error: request for member 'insert' in 'myTrainingOutputs', which is of non-class type 'std::__cxx11::list<int>()'
    myTrainingOutputs.insert(itTO,myOutputsA);
    Any help or suggestions would be appreciated.
    Thanks
    Mike

  2. #2
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    7,551
    Ok - think I figured it out. Really dumb mistakes on my part. Assumed too much.

    1. Declares should read:
    Code:
      std::list<float> myTrainingInputs;
      std::list<float> myTrainingOutputs;
    2. Changed insert to push_back and got rid of the iterators. For some reason it still didn't like the making the assignment:
    Code:
    itTI = myTrainingInputs.begin();
    3. When doing the push_back's forgot to add the index for the myInputs and myOutputs.

    Now on to the other errors. Guess this is a warning - when trying to convert from another program to c++ be careful and look first.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •