Jump to content

C++ Tutorial error?


Recommended Posts

I'm working my way thru the C++ tutorials and I've gotten to the if statement part.

#include "Leadwerks.h";
int main(int argc, const char *argv[])
{
    if (2>1)
    {
        Print("Two is greater than one!");
    }
}

Then the explanation of the code is given as.

"The above code tests to see if two is greater than one (it is!) and then goes on to execute the code inside the statement. The statement is terminated with the "end" keyword. So we know the above code would print out the word "true" when run."

Isn't this wrong?

The statement end with the } doesn't it? and not the "end" keyword. 2>1 is true but would print "Two is greater than one!" as we have told it to do if 2>1.

  • Thanks 1
Link to comment
Share on other sites

I might be being pedantic here so bear with me:

Good catch on the semicolon but those won't affect your program.  Toss some extra semicolons in your program between functions and it'll still compile.  Still, while it will run just fine, agreed that it's unnecessary.

As for the namespace thing, that's only if you include Leadwerks commands, right?  For these simple C++ examples, I don't believe it's necessary.  (He does talk about namespaces in the first C++ tutorial page.)

Finally, it should be printf, not Print.  Like, everywhere.  ?

  • Like 1
Link to comment
Share on other sites

Thanks gentlemen for the clarification. I thought it was a cut and paste error.

There is a couple of "s written as quote in the Loop section of the tutorial as well.

There is also a mention of Lua in the start of the Loop tutorial, not sure if thats a cut and paste issue.

I'm going OK just started arrays, vectors and containers. Its a bit of a step up from Lua but I'm feeling a little more comfortable with C++ than I thought I would.

 

Link to comment
Share on other sites

2 hours ago, reepblue said:

Yeah. There is a Leadwerks::Print(), but there is no such thing as Print() unless you make your own function.

Ah, I didn't know that.  Thanks.

1 hour ago, Thirsty Panther said:

I'm going OK just started arrays, vectors and containers. Its a bit of a step up from Lua but I'm feeling a little more comfortable with C++ than I thought I would.

Good job.  Seems you're picking it up fast.

  • Like 1
Link to comment
Share on other sites

Sensei I believe I have found another test.

In the Vector lesson should the code for a vector of integer values be 

std::vector <int> myvector;

and not

std::vector myvector;

And the code for a vector of strings should be 

std::vector <string> myvector;

 And in Lists should we also declare which data type we are going to use.

std::list <int> mylist;
  or
std::list <string> mylist;

As a general rule what are the strengths and weaknesses of arrays, vectors, maps and lists.

I assume arrays are less memory hungry as their size doesn't change but are less flexible.

Vectors look like they could be difficult to keep track of.

Maps look similar to how tables work in Lua.

  • Thanks 1
Link to comment
Share on other sites

9 hours ago, Thirsty Panther said:

As a general rule what are the strengths and weaknesses of arrays, vectors, maps and lists.

Arrays use a single continuous memory block. If you want to enlarge your array you need to copy all data to the new location. However, when you want to randomly index the array, this can be done in constant time, so accessing x[5] is just as fast as accessing the first element or the last one.

Vectors are basically a sort of managed arrays. They take care for reserving more space than you actually requested, so they can dynamically be increased and decreased in a limited manner without a performance hit.

Lists are usually linked one element to the next. This means, for accessing the 5th element, you need to call list->first->next->next->next->next. It becomes evident that random access on any member of the list is quite bad. However, if you only iterate over the entire list, handling each element in there, this can be done quite efficient. Of course, lists can be grown and shrinked without problems, since they do not have to be any continous blocks of memory. However, since you need to keep track of all the additional bookkeeping-elements, like references to the first, last, next and previous elements, you need more storage than in an array. Performance wise, the fact that they are not in a continuous block of memory also impacts the caching behaviour (but you probably won't notice unless you are using them quite intensively)

Maps are something quite different. They usually work by building a hash value of the corresponding keys and finding the corresponding list of elements that have the same hash-value in an array of buckets. They are usually well suited, if you have a key-value-pair, which you need to track. Both, insert- and lookup- operations require building of a hash-value, as well as an array access and iterating over a (usually very small) list of items in the corresponding bucket, so they have "rather constant" access times.

  • Like 1
Link to comment
Share on other sites

The only functions you'll need the Leadwerks namespace in front of the call is Time and Window. Make a precompiled/shared header and define the following.

#ifndef STDAFX_H
#define STDAFX_H
#if defined( _WIN32 )
#pragma once
#endif

#include "Leadwerks.h"
#ifdef GetFileType
#undef GetFileType
#endif
using namespace Leadwerks;

#define Timing Leadwerks::Time
#define EngineWindow Leadwerks::Window
  
#endif // STDAFX_H

Then whenever you want to use the Time or Window function you'll just call something like this:

#include "pch.h"

int main()
{ 
  auto w = EngineWindow::Create();

  while (true)
  {
      Timing::Update();
  }
}

This ensures that your code will be cross compatible and you don't have to ever worry about using the namespace.

  • Thanks 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...