C++ Tutorial for Beginners Ep#23 - Structs / Structures with Examples - SavvyNik on Linux

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ม.ค. 2025

ความคิดเห็น • 10

  • @SavvyNik
    @SavvyNik  ปีที่แล้ว +1

    My NEW in depth C++ Course is now available at - learn.savvynik.com/

  • @yondaime500
    @yondaime500 4 ปีที่แล้ว +5

    Just a quick note: std::endl is to be avoided in general. It implicitly flushes the line, which can kill performance depending on what you're doing. For printing a few lines to stdout it won't matter much, but in general it's better to print a newline character '
    ' and then call flush() when you actually want to flush. This is noted in the cppreference page for endl and in the C++ Core Guidelines under "SL.io.50: Avoid endl". It's a good thing to tell beginners so they don't make slow apps ;)

    • @SavvyNik
      @SavvyNik  4 ปีที่แล้ว

      Great mention!! Thanks for adding this. Really good to know when you start adding in debugging messages and logging tons of of info haha :D

  • @ilyabikmeev
    @ilyabikmeev 4 ปีที่แล้ว +1

    Hello! I got a question about templates. I goggled it but unfortunately I couldn't find the answer. We have a template class "List" and class "Node" as a private field in class List. Something like this :
    template
    class List{
    private:
    struct Node{
    Node* pNext;
    T data;
    } ;
    public:
    //constructor and so on
    void foo() ;
    } ;
    The question is: how can we create an object of struct Node inside "void foo"? I tried this:
    template
    void List::foo() {
    List::Node cur;
    }
    But it says that "Node wasn't declared in this scope"

    • @SavvyNik
      @SavvyNik  4 ปีที่แล้ว

      So foo() belongs to the class List and you are trying to create a Node structure inside of the member function declaration called foo? I assume you have a separate file for your member functions and perhaps the class template in your header file? Or are they all together. If the function foo() is defined somewhere outside the class and you're trying to reach a member variable that is private that could cause your issue. Does anything change if you move your structure definition to public. It's hard to tell what's going on without the full set of files & code.

    • @yondaime500
      @yondaime500 4 ปีที่แล้ว

      I pasted your code on godbolt and it compiles with the latest versions of gcc, clang and msvc. In fact, the List:: is not needed inside foo(), since you are already inside the class scope. But it should work either way.
      By the way, your Node struct is not a field, but a nested type. To make a field of type Node inside List, you have to add an instance name after the class definition, like "} mNode;" or add a line declaring the instance, like "Node mNode".

    • @ilyabikmeev
      @ilyabikmeev 4 ปีที่แล้ว

      @@yondaime500 Sorry, English isn't my native language that's why it's quite hard for me to explain it :) So, the task was to make a linked list and I desided to hide struct Node inside class List. I have to create an object of Node* because there're such methods as push_back, push_front and so on. And you need kind of "iterator" to do it - Node* cur , I mean:
      template
      class List{
      struct Node{
      Node* next;
      T data;
      Node(T _data):data(_data),next(nullptr){}
      }*head;
      int size;
      public:
      List(int _size): size(_size),head(nullptr){}
      ~List();
      void push_back(T data);
      };
      template
      void List::push_back(T data){
      if(!head)
      head = new Node(data);
      else{
      List::Node* curNode = head;
      while(curNode){
      curNode = curNode->next;
      }
      curNode->next = new Node(data);
      }
      size++;
      }
      I didn't show the whole code because I only have problems in this moment. It says that "Node wasn't declared on this scope". And if I did this class non template - there wouldn't be such problems and I don't know why. If you want to help me and you need the whole code - can you write your email? I'd be so glad :)

    • @yondaime500
      @yondaime500 4 ปีที่แล้ว

      @@ilyabikmeev godbolt doesn't seem to have a problem with this code either: godbolt.org/z/1bGGqv
      Can you reproduce the error there?

  • @Raam1827
    @Raam1827 ปีที่แล้ว

    In this video you not used string header but not get any errors why

  • @yumyum3266
    @yumyum3266 4 ปีที่แล้ว

    😋