Results 1 to 5 of 5

Thread: C++ struct help :(

  1. #1
    I R Toff Pandi! TAKTAK's Avatar
    Join Date
    Mar 2008
    Location
    Vergon6
    Posts
    7,450
    Thanks
    553
    Thanked
    1,013 times in 748 posts
    • TAKTAK's system
      • Motherboard:
      • ASUS ROG STRIX B450-F GAMING
      • CPU:
      • Ryzen 7 3700X
      • Memory:
      • 16GB Corsair Vengeance LPX 3200MHz
      • Storage:
      • 500GB Samsung 970 EVO
      • Graphics card(s):
      • 5700 XT 50th Anniversary
      • PSU:
      • Be Quiet SFX-L 600W
      • Case:
      • Lian Li PC-O11 Mini
      • Operating System:
      • Windows 10
      • Monitor(s):
      • LG Ultrawide
      • Internet:
      • 200Mb FTTP

    C++ struct help :(

    I needs c++ help
    I need to return 3 values (1 int, 2 strings) from a function, one of which is being returned direct by the return function (the int). So i'm employing a struct for the final 2.
    I can pull the values from the struct back within the same function, but I can't pull them back in other functions, it just returns blank.

    I have a struct in a header file, populated by one function via an object i.e.

    int function1()
    {
    structname structObj
    structObj.firstval = value
    structObj.secondval = value2
    }

    int function2()
    {
    structname structObj
    cout << structObj.firstval
    cout << structObj.secondval
    }

    How do I pull the values out in function 2?
    Pointers? How do I set the pointers up correctly for a struct? I'm guessing it's something easy that i'm missing but I just can't see the wood for the trees right now
    Post Counts and Other Rewards, Rules, Folding@Home, Fans: Push vs Pull vs Push-Pull, Corsair PSU OEMs.

    Quote Originally Posted by razer121 View Post
    Would you like me to enter you? it would be my pleasure
    TAKTAK.co.uk

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ struct help :(

    Just return the struct like any other value:
    Code:
    struct sStruct {
    	int id;
    	string str1;
    	string str2;
    };
    
    sStruct setStruct(int id, string str1, string str2) {
    	sStruct setStruct;
    	setStruct.id = id;
    	setStruct.str1 = str1;
    	setStruct.str2 = str2;
    
    	return setStruct; 
    }
    
    void printStruct(sStruct printStruct) {
    	cout << printStruct.str1 << printStruct.str2 << endl;
    }
    
    int main() {
    	sStruct mainStruct;
    
    	mainStruct = setStruct(1, "string 1", "string 2");
    	printStruct(mainStruct);
    
    	return 0;
    }
    Tested, it works.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. Received thanks from:

    TAKTAK (25-03-2012)

  4. #3
    I R Toff Pandi! TAKTAK's Avatar
    Join Date
    Mar 2008
    Location
    Vergon6
    Posts
    7,450
    Thanks
    553
    Thanked
    1,013 times in 748 posts
    • TAKTAK's system
      • Motherboard:
      • ASUS ROG STRIX B450-F GAMING
      • CPU:
      • Ryzen 7 3700X
      • Memory:
      • 16GB Corsair Vengeance LPX 3200MHz
      • Storage:
      • 500GB Samsung 970 EVO
      • Graphics card(s):
      • 5700 XT 50th Anniversary
      • PSU:
      • Be Quiet SFX-L 600W
      • Case:
      • Lian Li PC-O11 Mini
      • Operating System:
      • Windows 10
      • Monitor(s):
      • LG Ultrawide
      • Internet:
      • 200Mb FTTP

    Re: C++ struct help :(

    Aaaaaaah, I see

    Thankyou very much, in the end I moved my filename grabbing to the next function, which does some work on the binary files, before I saw your reply
    Post Counts and Other Rewards, Rules, Folding@Home, Fans: Push vs Pull vs Push-Pull, Corsair PSU OEMs.

    Quote Originally Posted by razer121 View Post
    Would you like me to enter you? it would be my pleasure
    TAKTAK.co.uk

  5. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: C++ struct help :(

    I'm confused.

    You've got a local struct in two methods, so its garenteed to be different as its stack alocated.

    You then want to get the result from the first functions changes?

    surely you then want to reutrn the struct in the first func, rather than int?

    edit: lolwut a 12 hour delay in chrome posting this?
    throw new ArgumentException (String, String, Exception)

  6. #5
    I R Toff Pandi! TAKTAK's Avatar
    Join Date
    Mar 2008
    Location
    Vergon6
    Posts
    7,450
    Thanks
    553
    Thanked
    1,013 times in 748 posts
    • TAKTAK's system
      • Motherboard:
      • ASUS ROG STRIX B450-F GAMING
      • CPU:
      • Ryzen 7 3700X
      • Memory:
      • 16GB Corsair Vengeance LPX 3200MHz
      • Storage:
      • 500GB Samsung 970 EVO
      • Graphics card(s):
      • 5700 XT 50th Anniversary
      • PSU:
      • Be Quiet SFX-L 600W
      • Case:
      • Lian Li PC-O11 Mini
      • Operating System:
      • Windows 10
      • Monitor(s):
      • LG Ultrawide
      • Internet:
      • 200Mb FTTP

    Re: C++ struct help :(

    Yeah I was being an eedjit expecting the struct to magically contain the values even though I never actually passed them accross, I only ever populated it for that specific function. Aaah well, too much coursework at once to fix it. It does what the brief asks for so I guess it's okay..
    I guess it's something I can mention in the exam in an hour..
    Post Counts and Other Rewards, Rules, Folding@Home, Fans: Push vs Pull vs Push-Pull, Corsair PSU OEMs.

    Quote Originally Posted by razer121 View Post
    Would you like me to enter you? it would be my pleasure
    TAKTAK.co.uk

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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