Thread: was not declared in this scope

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    31

    was not declared in this scope

    Code:
     std::string PacketBuilder::Get()
    {
        std::string retdata;
        util::pairchar id = PacketProcessor::EPID(this->id);
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (id[1] != 255));
    
    
    
    
        int extra = (id[1] != 255);
     unsigned short sequence =  0;
        int bytes_ = 1;
    
    
        std::string seqdata = "";
    
    
        if(extra)
        {
        sequence = Sequence ();
    
    
            if(sequence >= 253)
                bytes_ = 2;
    
    
            util::quadchar seq_ = PacketProcessor::ENumber(sequence);
    
    
            if(bytes_ == 1)
            {
                 seqdata += seq_[0];
            }
            else
            {
                seqdata += seq_[0];
                seqdata += seq_[1];
            }
        }
    
    
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (extra + bytes_ -1));
    
    
        retdata += length[0];
        retdata += length[1];
        retdata += id[0];
        retdata += id[1];
        retdata += seqdata;
        retdata += this->data;
        return retdata;
    }
    does anyone know why I'm getting this error?


    error : 'sequence' was not declared in this scope

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the exact error message and which line does it refer to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    The line is ///


    sequence = Sequence ();


    The error is ///

    error : 'sequence' was not declared in this scope

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As you are probably aware, the error message does not make sense. Are you copying and pasting the error message? I'm assuming that you are, but it is good to check in case you happened to change "Sequence" to "sequence", which in this context changes things significantly.

    If you did copy and paste the error message, then I'd try a rebuild of the source file, in case there's some kind of caching problem going on with your build system. If that still doesn't work, then I suggest that you come up with the smallest and simplest program that you expect should compile, but which demonstrates the error (i.e., this should be something that members here can try to compile without needing anything other than the standard library, so they can see the problem for themselves). Right now, we are purely dependent on you to compile the code and report the error message accurately, so if you have made any mistakes in doing so, then we would be none the wiser.

    By the way, although it might not matter, which compiler are you using and against what version of the C++ standard are you compiling?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    Sorry I wasn't actually copying and pasting error message, it actually is

    error : 'Sequence' was not declared in this scope

    With a capital S instead of lowercase

    I tried to rebuild the source file but no luck




    Also I don't think I'm going to be able to make a program which demonstrates the error..


    I am using mingw g++ compiler and I'm not sure which version of the c++ standard

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, then the error message makes perfect sense: Sequence was not declared, just as it says. You may have defined it below this code or in another file, in which case you need a forward declaration before calling it. (Or if it is a class, then the class definition must be available before that point, but I'm guessing Sequence is a function rather than a class with a conversion to unsigned short.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    Yes it is declared in another file called packet.hpp so how do I go about a forward declaration, before calling it?

    Could you give me an example please?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's easy: #include "packet.hpp"

    I'm assuming that you're using the common convention of ".hpp" denoting a header file, and it is the very purpose of a header file to contain declarations to be included in source files. If you didn't write the header file correctly, then you might find yourself facing say, a linker error later, but we can help you fix that should it happen (but you'll then have to show us the header file).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    Sorry #include "packet.hpp" is already in my code so I'm guessing Sequence isn't declared like I thought it was.. so how would I declare Sequence in the original code that I posted earlier?

  10. #10
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    Meaning how would I declare 'Sequence' in this code


    Code:
      std::string PacketBuilder::Get()
    {
        std::string retdata;
        util::pairchar id = PacketProcessor::EPID(this->id);
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (id[1] != 255));
    
    
    
    
        int extra = (id[1] != 255);
     unsigned short sequence =  0;
        int bytes_ = 1;
    
    
        std::string seqdata = "";
    
    
        if(extra)
        {
        sequence = Sequence ();
    
    
            if(sequence >= 253)
                bytes_ = 2;
    
    
            util::quadchar seq_ = PacketProcessor::ENumber(sequence);
    
    
            if(bytes_ == 1)
            {
                 seqdata += seq_[0];
            }
            else
            {
                seqdata += seq_[0];
                seqdata += seq_[1];
            }
        }
    
    
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (extra + bytes_ -1));
    
    
        retdata += length[0];
        retdata += length[1];
        retdata += id[0];
        retdata += id[1];
        retdata += seqdata;
        retdata += this->data;
        return retdata;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kotacoder
    so how would I declare Sequence in the original code that I posted earlier?
    What is Sequence?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    error : 'Sequence' was not declared in this scope




    How would I declare Sequence before this line



    sequence = Sequence ();

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't know. I don't even know what Sequence is for sure other than it looks like a function because it was used with function-call syntax.

    Basically, to declare something is to describe to the compiler what it looks like. You're asking me how to declare it, but I don't know what it looks like because I haven't seen its declaration. It's kind of a chicken and egg scenario.

    That's what I asked you what is Sequence. For example, you could say "it is a function that has no parameters and returns an unsigned short". In that case, you could declare it like this:
    Code:
    unsigned short Sequence();
    but is this correct? That depends on what Sequence is, so you need to tell me what Sequence is.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2021
    Posts
    31
    Ok sorry for the misunderstanding, also thank you very much for helping me I am noob -.- when it comes to programming haha but I am learning slowly


    Honestly I'm not for sure even myself what Sequence is..


    All I know is I need to declare it some place so I'm going to try to declare it like you said
    Code:
     unsigned short Sequence();
    does that mean I declare it above the other code like this?

    Code:
    
    unsigned short Sequence();
    
    
      std::string PacketBuilder::Get()
    {
        std::string retdata;
        util::pairchar id = PacketProcessor::EPID(this->id);
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (id[1] != 255));
    
    
    
    
        int extra = (id[1] != 255);
     unsigned short sequence =  0;
        int bytes_ = 1;
    
    
        std::string seqdata = "";
    
    
        if(extra)
        {
        sequence = Sequence ();
    
    
            if(sequence >= 253)
                bytes_ = 2;
    
    
            util::quadchar seq_ = PacketProcessor::ENumber(sequence);
    
    
            if(bytes_ == 1)
            {
                 seqdata += seq_[0];
            }
            else
            {
                seqdata += seq_[0];
                seqdata += seq_[1];
            }
        }
    
    
        util::quadchar length = PacketProcessor::ENumber(this->length + 2 + (extra + bytes_ -1));
    
    
        retdata += length[0];
        retdata += length[1];
        retdata += id[0];
        retdata += id[1];
        retdata += seqdata;
        retdata += this->data;
        return retdata;
    }
    Or where should I declare it exactly?

    Btw sorry for all the noob questions, hopefully one day I will be better and not have to ask so many questions

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kotacoder
    I'm not for sure even myself what Sequence is
    Then why did you write this line of code?
    Code:
    sequence = Sequence ();
    If you don't know what Sequence is, then you don't know what that line of code does.

    Quote Originally Posted by kotacoder
    All I know is I need to declare it some place so I'm going to try to declare it like you said
    No, you should find out, e.g., search the rest of the code. You cannot just declare something blindly and expect it to work. (Or rather, you can, but you're probably going to find out that your expectations were misplaced.)

    Quote Originally Posted by kotacoder
    does that mean I declare it above the other code like this?
    If that is indeed the correct declaration of Sequence, then yes, you can put a forward declaration there.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setw not declared in this scope
    By sprankles in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2011, 07:49 PM
  2. c was not declared in this scope
    By altf4thc in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2010, 01:47 PM
  3. ‘max_element’ was not declared in this scope
    By Elya in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 10:54 AM
  4. Object not declared in this scope
    By -EquinoX- in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2009, 01:31 AM
  5. Not declared in this scope
    By Taka in forum C++ Programming
    Replies: 45
    Last Post: 03-13-2009, 05:36 AM

Tags for this Thread