MarcusAseth

Error when returning a Pair<type,type> from a function

Recommended Posts

Maybe today I'm confused and I'm missing the obvious...

I've made this simple struct inside my Utility.h 

template<typename TFirst, typename TSecond>
	struct Pair {
		Pair(TFirst f, TSecond s):First{f},Second{s}{}
		TFirst First;
		TSecond Second;
	};

And I'm using it from App.h (which #include "Utility.h") like so:

App.h

Pair<Uint32, Uint32> GetWindowSize();

App.cpp

Pair<Uint32, Uint32> App::GetWindowSize()
{
	return Pair<Uint32, Uint32>(Width,Height);
}

And I'm getting a cascade of errors ALL on line 39 (image below).

Any idea what I am doing wrong this time? x_x

eyvZQu7.png

 

EDIT: please mod delete this topic, I just remembered Utility.h put everything inside namespace util, sorry x_x

Edited by MarcusAseth

Share this post


Link to post
Share on other sites

Why not use std::pair, which is significantly more full-featured and optimal?

Why are you using a pair to return a window size? A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept. This seems like a prematurely-pessimized API design.

Share this post


Link to post
Share on other sites

I didn't wanted to have to add another include in all my files in order to use that, since I already had my Utility.h included I put my class inside of that. There is a way to add the std::pair to my Utility.h so that all files including it get to use std::pair without including anything else?

Quote

A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

EDIT: by the way, why the code below is being pasted in this crazy way?!  Maybe a problem in how the tab character is interpreted?

It should appear like this --> https://pastebin.com/iddWWWfH

GameMode::GameMode(SDL_Window* Window, SDL_Renderer* Renderer, App* App)
	:Window{ Window }, Renderer{ Renderer }, AppRef{ App }, Running{ true }
{
	float WindowWidth = static_cast<float>(AppRef->GetWindowSize().First);
	float WindowHeight = static_cast<float>(AppRef->GetWindowSize().Second);
	
	//PhysicsManager
	PhysicsManager = std::make_unique<Physics>(this);

	//Create Player
	Entities.push_back(std::make_unique<Paddle>(this,
												static_cast<int>(WindowWidth) / 2,
												static_cast<int>(WindowHeight) - 40,
												PivotMode::CENTER));

	//Boundaries
	//Right
	Entities.push_back(std::make_unique<Boundary>(this,
												  Rect{ WindowWidth - 5.f, 0.f,
													   50.f, WindowHeight },
												  PivotMode::CENTER));
	//Left
	Entities.push_back(std::make_unique<Boundary>(this,
												  Rect{ -45.f, 0.f,
												  50.f, WindowHeight },
												  PivotMode::CENTER));
	//Top
	Entities.push_back(std::make_unique<Boundary>(this,
												  Rect{ 6.f, -45.f,
												  WindowWidth - 12.f, 50.f },
												  PivotMode::CENTER));

	//Bottom
	Entities.push_back(std::make_unique<Boundary>(this,
												  Rect{ 6.f, WindowHeight - 5.f,
												  WindowWidth - 12.f, 50.f },
												  PivotMode::CENTER));

	//Create Ball
	Entities.push_back(std::make_unique<Ball>(this,
											  static_cast<int>(WindowWidth) / 2,
											  200,
											  PivotMode::CENTER));
}

 

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
11 minutes ago, MarcusAseth said:

EDIT: by the way, why the code below is being pasted in this crazy way?!  Maybe a problem in how the tab character is interpreted?

It is, a TAB character is defined to aligned at multiples of 8 characters, in ASCII. Most editors ignore this definition, and allow you to change it, giving the illusion that a TAB is whatever you configured. That of course fails as soon as your file leaves your editor, since other programs or other people use different TAB settings, or use the official size.

Share this post


Link to post
Share on other sites
2 minutes ago, Alberth said:

It is, a TAB character is defined to aligned at multiples of 8 characters, in ASCII. Most editors ignore this definition, and allow you to change it, giving the illusion that a TAB is whatever you configured. That of course fails as soon as your file leaves your editor, since other programs or other people use different TAB settings, or use the official size.

though this pastes correctly on codeshare.io and pastebin and Windows blocknote and pretty much everywhere else... wouldn't be better to have the site behaving the same way? :P

Share this post


Link to post
Share on other sites
Just now, MarcusAseth said:

though this pastes correctly on codeshare.io and pastebin and Windows blocknote

It can't paste correctly in all cases. TAB length isn't copied with the text, so either it guestimates, or it just happens to use the same length as you have. In both cases, you can break the formatting with a clever enough example.

 

24 minutes ago, MarcusAseth said:
Quote

A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

This means that if two numbers have a width and height relation, you can discuss other properties, eg "area". You can multiply 2 unrelated numbers, with each other, but without a width/height relation between them it has no useful meaning.

Share this post


Link to post
Share on other sites
58 minutes ago, MarcusAseth said:

I didn't wanted to have to add another include in all my files in order to use that, since I already had my Utility.h included I put my class inside of that. There is a way to add the std::pair to my Utility.h so that all files including it get to use std::pair without including anything else?

No, but including <utility> isn't going to be a huge deal, especially if you're just taking your Utility.h and turning into a convenience catch-all anyway. The net impact on compile time over the life of any nontrivial project will probably be smaller using <utility>, since it changes so infrequently, whereas your Utility.h will change quite often.

1 hour ago, MarcusAseth said:

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

Why do you have a Window class then? Why not Tuple<std::string, int, int, Pair<int, int>, bool, bool, ...>? It's the same thing, right?

Seeing "Size" in a function signature conveys useful meaning that seeing "Pair<foo, bar>" does not. It also lets you write interfaces that operate in the language appropriate for the semantics of the data you're sending around - as Alberth noted with his "area" example, but also even simpler than that: what is a size? It's a width and a height. Not a "first" and a "second." Seeing "thing.width" written in code is lot easier to comprehend than "thing.first," just as seeing "Size" in the parameter list is easier to comprehend than "Pair."

Share this post


Link to post
Share on other sites

ok, so the bottom line is I should either return a Struct filled with info with inside appropriete variables names like WindowInfo.width and WindowInfo.height or split it into two methods Game->GetWindowWidth() , Game->GetWindowHeight()

Did I got this right? :D

By the way, I added this as well into Utility.h

	template <typename First, typename Second>
	using Pair = std::pair<First, Second>;

and I'm glad to see it everything still compiles without any changes required :P

Edited by MarcusAseth

Share this post


Link to post
Share on other sites
1 hour ago, MarcusAseth said:

Did I got this right?

Yes.

I'd vote for the first option, since about 80% of the time if you want the width you'll probably also want the height, and there really isn't much to worry about in terms of performance here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Forum Statistics

    • Total Topics
      627719
    • Total Posts
      2978797
  • Similar Content

    • By SR D
      I've been learning how to do vertex buffers plus index buffers using Ogre, but I believe this is mostly the same across several engines. I have this question about using vertex buffers + index buffers.
      Using DynamicGeometryGameState (from Ogre) as an example, I noticed that when drawing the cubes, they were programmatically drawn in order within the createIndexBuffer() function like so ...
       
      const Ogre::uint16 c_indexData[3 * 2 * 6] = { 0, 1, 2, 2, 3, 0, //Front face 6, 5, 4, 4, 7, 6, //Back face 3, 2, 6, 6, 7, 3, //Top face 5, 1, 0, 0, 4, 5, //Bottom face 4, 0, 3, 3, 7, 4, //Left face 6, 2, 1, 1, 5, 6, //Right face };
      From the above, the front face is drawn using the vertices 0, 1, 2, 2, 3, 0. But when reading in thousands of vertices from a file, one obviously doesn't code an array specifying which vertices make up a face.
      So how is this done when working with a large number of vertices?
    • By Josheir
      I am working on a SFML c++ program that uses two rendering windows passed from main to the function drawmessage in a class textmessage.  I was using the second window for displaying debug information that is displayed because I could not get the appropriate information from the SFML object.
      With that said, here is the part of that function that works the first time through and does not on the second usage.  I really have changed the code to try and get it working.   For example I created the two objects locally here for testing.  I am sorry about the extra commented statements they help convey the message too.
      There is the same problem though, the statement :     string test =     message_holder10.getString(); is working and shows "asty" on every run.  On the first run of the program there is a display of the text correctly however on the second call there is no display of it.  (I am stepping through until the display command.)
      I feel like I have exhausted my tries so I am asking for help please.
      If it is the font I will just die, I really don't think it is.
       
       
       
                  sf::Text message_holder10;
                  sf::RenderWindow windowtype3(sf::VideoMode(700, 1000), "a");

                  if ((space_is_used && on_last_line) || (space_is_used && ((line_number) == (total_lines - 2))))
                  {

                      //string temp_string = message::Get_Out_Bound_String();
                      //int length_of_string = temp_string.length();
                      sf::Font Fontforscore;
                      if (gflag == 0)
                      {
                          gflag = 1;
                          
                          if (!Fontforscore.loadFromFile("ARIALBD.ttf"))
                          {
                              exit(1);
                          }

                          message_holder10.setFont(Fontforscore);
                          message_holder10.setCharacterSize(100);
                          message_holder10.setFillColor(sf::Color::Red);
                          message_holder10.setOrigin(0, 0);
                          message_holder10.setPosition(0, 0);
                          windowtype2.close();
                      }
                      message_holder10.setString("asty");
                          
                          //int y_for_space = display_y_setting + (total_lines - 2) * each_vertical_offset_is;
                          //int this_width = 0;
                          
                          //float x = message_holder.getLocalBounds().width;

                          
                          
                          //message_holder.setPosition( ( (first_width - x )/2), y_for_space);
                          
                  
                          //windowtype2.close();

                          string test =     message_holder10.getString();
                          
                          windowtype3.clear();
                          windowtype3.draw(message_holder10);
                          windowtype3.display();
                          
                          
                           
       
                          //windowtype.display();
                      

                       Wait_For_Space_Press();
                      
       
      /////////////////////////
       
      Before, the :      windowtype3.display()  without the clear was drawing other text in this call, just not this one particular text message with it!
       
      Thank you so much I am wondering what it can be,
       
      Josheir
    • By Tispe
      Hi
      I want to test out a polymorphic entity component system where the idea is that the components of an entity are "compositioned" using templated multiple inheritance. But I am running into an issue because I am stacking a bunch of methods with the same names inside a class (but they have different signatures). I want these methods to be overloaded by the template type but my compiler says the access is ambiguous. I have issues making them unambiguous with the using declaration because the paramter pack expansion causes a syntax error.
      Can anyone here give me some advice on this?
       
      template <class T> class component { T m_data; protected: component() {}; ~component() {}; public: void set(const T& data) { m_data = data; }; }; template <class ...Ts> class entity : public component<Ts>... { public: entity() {}; ~entity() {}; //using component<Ts>::set...; // syntax error }; struct position { float x{}; float y{}; float z{}; }; struct velocity { float x{}; float y{}; float z{}; }; int main() { entity<position, velocity> myEntity; position pos = { 1.0f, 1.0f, 1.0f }; velocity vel = { 2.0f, 2.0f, 2.0f }; myEntity.set(pos); // error C2385: ambiguous access of 'set' //myEntity.set(vel); return 0; }  
    • By Baemz
      Hello,
      I've been working on some culling-techniques for a project. We've built our own engine so pretty much everything is built from scratch. I've set up a frustum with the following code, assuming that the FOV is 90 degrees.
      float angle = CU::ToRadians(45.f); Plane<float> nearPlane(Vector3<float>(0, 0, aNear), Vector3<float>(0, 0, -1)); Plane<float> farPlane(Vector3<float>(0, 0, aFar), Vector3<float>(0, 0, 1)); Plane<float> right(Vector3<float>(0, 0, 0), Vector3<float>(angle, 0, -angle)); Plane<float> left(Vector3<float>(0, 0, 0), Vector3<float>(-angle, 0, -angle)); Plane<float> up(Vector3<float>(0, 0, 0), Vector3<float>(0, angle, -angle)); Plane<float> down(Vector3<float>(0, 0, 0), Vector3<float>(0, -angle, -angle)); myVolume.AddPlane(nearPlane); myVolume.AddPlane(farPlane); myVolume.AddPlane(right); myVolume.AddPlane(left); myVolume.AddPlane(up); myVolume.AddPlane(down); When checking the intersections I am using a BoundingSphere of my models, which is calculated by taking the average position of all vertices and then choosing the furthest distance to a vertex for radius. The actual intersection test looks like this, where the "myFrustum90" is the actual frustum described above.
      The orientationInverse is the viewMatrix in this case.
      bool CFrustum::Intersects(const SFrustumCollider& aCollider) { CU::Vector4<float> position = CU::Vector4<float>(aCollider.myCenter.x, aCollider.myCenter.y, aCollider.myCenter.z, 1.f) * myOrientationInverse; return myFrustum90.Inside({ position.x, position.y, position.z }, aCollider.myRadius); } The Inside() function looks like this.
      template <typename T> bool PlaneVolume<T>::Inside(Vector3<T> aPosition, T aRadius) const { for (unsigned short i = 0; i < myPlaneList.size(); ++i) { if (myPlaneList[i].ClassifySpherePlane(aPosition, aRadius) > 0) { return false; } } return true; } And this is the ClassifySpherePlane() function. (The plane is defined as a Vector4 called myABCD, where ABC is the normal)
      template <typename T> inline int Plane<T>::ClassifySpherePlane(Vector3<T> aSpherePosition, float aSphereRadius) const { float distance = (aSpherePosition.Dot(myNormal)) - myABCD.w; // completely on the front side if (distance >= aSphereRadius) { return 1; } // completely on the backside (aka "inside") if (distance <= -aSphereRadius) { return -1; } //sphere intersects the plane return 0; }  
      Please bare in mind that this code is not optimized nor well-written by any means. I am just looking to get it working.
      The result of this culling is that the models seem to be culled a bit "too early", so that the culling is visible and the models pops away.
      How do I get the culling to work properly?
      I have tried different techniques but haven't gotten any of them to work.
      If you need more code or explanations feel free to ask for it.

      Thanks.
       
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
  • Popular Now