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. Help with imple syntax error.

Help with imple syntax error.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++visual-studio
13 Posts 4 Posters 2 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.
  • L Offline
    L Offline
    Lord Kixdemp
    wrote on last edited by
    #1

    Hello everyone! :-D OK, quick question; int *livesPos; // ... later in code, inside a function livesPos = {30, 15}; What's the correct way to do this? VS gives me "syntax error : '{'". Thanks! ;) (PS: This is C, not C++)

    Windows Calculator told me I will die at 28. :(

    R 1 Reply Last reply
    0
    • L Lord Kixdemp

      Hello everyone! :-D OK, quick question; int *livesPos; // ... later in code, inside a function livesPos = {30, 15}; What's the correct way to do this? VS gives me "syntax error : '{'". Thanks! ;) (PS: This is C, not C++)

      Windows Calculator told me I will die at 28. :(

      R Offline
      R Offline
      rondznt
      wrote on last edited by
      #2

      int livesPos[] = {30, 15}; the '{' can only be used for variable instantiation and not later. :suss:

      L 1 Reply Last reply
      0
      • R rondznt

        int livesPos[] = {30, 15}; the '{' can only be used for variable instantiation and not later. :suss:

        L Offline
        L Offline
        Lord Kixdemp
        wrote on last edited by
        #3

        But, livesPos is a global variable... And let's assume that 30 and 15 are variables, I'll get some error about not being able to init globals without constant values... :( So how would I do this? Thanks! ;)

        Windows Calculator told me I will die at 28. :(

        C 1 Reply Last reply
        0
        • L Lord Kixdemp

          But, livesPos is a global variable... And let's assume that 30 and 15 are variables, I'll get some error about not being able to init globals without constant values... :( So how would I do this? Thanks! ;)

          Windows Calculator told me I will die at 28. :(

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          one good way would be to not have globals. Another would be to use a vector. A third would be livesPos = new int[2]; livesPos[0] = 5; livesPos[1] = 666; or whatever.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          L A 2 Replies Last reply
          0
          • C Christian Graus

            one good way would be to not have globals. Another would be to use a vector. A third would be livesPos = new int[2]; livesPos[0] = 5; livesPos[1] = 666; or whatever.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            L Offline
            L Offline
            Lord Kixdemp
            wrote on last edited by
            #5

            Christian Graus? You again?! :wtf: I don't think there's ever been a thread by me without you in it... Haha, you do live here, don't you? ;) Anyway, why do people always say not to use globals? I need file "interoperability". :P What should I use then? Thanks! ;)

            Windows Calculator told me I will die at 28. :(

            C 1 Reply Last reply
            0
            • C Christian Graus

              one good way would be to not have globals. Another would be to use a vector. A third would be livesPos = new int[2]; livesPos[0] = 5; livesPos[1] = 666; or whatever.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              A Offline
              A Offline
              Anthony Mushrow
              wrote on last edited by
              #6

              What would you do without global variables?

              L C 2 Replies Last reply
              0
              • A Anthony Mushrow

                What would you do without global variables?

                L Offline
                L Offline
                Lord Kixdemp
                wrote on last edited by
                #7

                Well, I would have all my variables available for all my functions without having to pass any annoying parameters... ;P But now I see this doesn't work very well in C... So what should I do, put all of them in main() and pass pointers to my functions? Or how do you do it? Thanks! ;)

                Windows Calculator told me I will die at 28. :(

                1 Reply Last reply
                0
                • L Lord Kixdemp

                  Christian Graus? You again?! :wtf: I don't think there's ever been a thread by me without you in it... Haha, you do live here, don't you? ;) Anyway, why do people always say not to use globals? I need file "interoperability". :P What should I use then? Thanks! ;)

                  Windows Calculator told me I will die at 28. :(

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  The problem with a global is that you can't debug it, or control it. At a minimum, create a class that has public static methods to get and set variables which are private. That way, you can set breakpoints if you need to debug access to the values. Even better, create a class with protected members, and make the classes you want to grant access, friends of that class.

                  Lord Kixdemp wrote:

                  you do live here, don't you?

                  *grin* 14+ hours a day, yeah.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  R 1 Reply Last reply
                  0
                  • A Anthony Mushrow

                    What would you do without global variables?

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    Write maintainable code ?

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    L 1 Reply Last reply
                    0
                    • C Christian Graus

                      The problem with a global is that you can't debug it, or control it. At a minimum, create a class that has public static methods to get and set variables which are private. That way, you can set breakpoints if you need to debug access to the values. Even better, create a class with protected members, and make the classes you want to grant access, friends of that class.

                      Lord Kixdemp wrote:

                      you do live here, don't you?

                      *grin* 14+ hours a day, yeah.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      R Offline
                      R Offline
                      rondznt
                      wrote on last edited by
                      #10

                      That is all very nice and true only one problem: this guy said he used C and not C++.. I guess u could some OO implementation in C (header represents a class and each function takes a pointer to structure that represents 'this'), still u don't have encapsulation...

                      C 1 Reply Last reply
                      0
                      • R rondznt

                        That is all very nice and true only one problem: this guy said he used C and not C++.. I guess u could some OO implementation in C (header represents a class and each function takes a pointer to structure that represents 'this'), still u don't have encapsulation...

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        Oh, I missed that. Then, there's no way around it. C kind of sucks that way.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        1 Reply Last reply
                        0
                        • C Christian Graus

                          Write maintainable code ?

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          L Offline
                          L Offline
                          Lord Kixdemp
                          wrote on last edited by
                          #12

                          Maintainable code? What do you mean? :wtf:

                          Windows Calculator told me I will die at 28. :(

                          C 1 Reply Last reply
                          0
                          • L Lord Kixdemp

                            Maintainable code? What do you mean? :wtf:

                            Windows Calculator told me I will die at 28. :(

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #13

                            It means, code that is easy to work on if you have to come back to it later.

                            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                            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