#Beginner with confusing error on simple text game course

156 messages · Page 1 of 1 (latest)

hollow lynx
#

Hey! I recently started learning C++ through Venza Academy courses, and while the introductory course on basics was pretty reasonable to pick up, I then moved onto their second course that runs you through making a simple text based game and I'm just having a terrible time near the end of it.

The pace of their videos is just really rushed, so much so that the teacher (who seems great, to be fair) is clearly just trying desperately to fit each video into a 10 minute chunk, and as a result isn't able to explain much of anything, making errors of his own and needing to jump around to fix things throughout the course, etc.

So here I am at the end of the course and despite following things (I thought) to the letter, I'm looking at list of errors that the course has just not given me any skills on how to fix. The main one is definitely a constructor for a Room class not working as it should, but I'm just utterly clueless of how to fix the issue since I've followed the course exactly on how he set it up, as far as I can tell.

I would not doubt that I screwed something up myself, of course, but it sounds like this course may have not gotten me off on the right foot on a few practices that were probably more focused towards making the teaching process quicker. @cerulean lagoon suggested I make a thread here in hopes that this silly little project can be salvaged to some extent, so here I am.

tame crescentBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hollow lynx
amber dawn
#

Did you try debugging?

hollow lynx
amber dawn
#

There are debugging tutorials out there. If you are on Windows you are better off using Visual studio.

hollow lynx
#

To this point I have just followed two courses, which had me using Visual Studio Code, so

cerulean lagoon
#

As mentioned before you should start by compiling

#

It's kind of pointless to help if you don't have errors to fix

hollow lynx
#

I mean, I put the main.cpp together and tried to run it, which obviously just threw the errors and didn't go anywhere. By "compile" I thought that just meant... running the main.cpp?

#

It's all put together and should run, according to the course, but obviously I've got issues.

#

The final part of the course just had me run the main.cpp in VSC and that was it, so if there's another layer to compiling something I just am not sure what that is yet haha

#
In file included from room.h:2,
                 from player.h:1,
                 from dungeon.cpp:2,
                 from main.cpp:5:
item.h:5:7: error: redefinition of 'class Item'
    5 | class Item
      |       ^~~~
In file included from room.h:2,
                 from player.h:1,
                 from dungeon.h:1,
                 from dungeon.cpp:1:
item.h:5:7: note: previous definition of 'class Item'
    5 | class Item
      |       ^~~~
In file included from room.h:2,
                 from player.h:1,
                 from player.cpp:1,
                 from main.cpp:6:
item.h:5:7: error: redefinition of 'class Item'
    5 | class Item
      |       ^~~~
item.h:5:7: note: previous definition of 'class Item'
    5 | class Item
      |       ^~~~
In file included from item.cpp:1,
                 from main.cpp:8:
item.h:5:7: error: redefinition of 'class Item'
    5 | class Item
      |       ^~~~
item.h:5:7: note: previous definition of 'class Item'
    5 | class Item
      |       ^~~~
In file included from Room.h:2,
                 from room.cpp:1,
                 from main.cpp:9:
item.h:5:7: error: redefinition of 'class Item'
    5 | class Item
      |       ^~~~
item.h:5:7: note: previous definition of 'class Item'
    5 | class Item
      |       ^~~~```
#

This being the result of trying to run, of course

amber dawn
#

This looks like a multiple inclusions error.

#

But the very first lines look very sus, why are you including a .cpp file?

hollow lynx
#

Yeah, and I've sifted through the relevant parts, but I don't see what I flubbed in relation to how the instructor set it up

#

That is what the course told me to do x_x

#

I'm just now learning that's bad, I guess

elder lava
#

Are you sure you need to include .cpp files in main.cpp, and not .h files?

hollow lynx
elder lava
#

From just the first two .cpp include lines it's already going to define everything in player.h twice

hollow lynx
#

Literally what he tells you to do in this course, haha

amber dawn
#

That's a screenshot from the course material?

hollow lynx
#

Yes

amber dawn
#

Ditch that course, right now.

hollow lynx
#

Oof

amber dawn
#

This person has no idea what they are talking about.

elder lava
#

Each .cpp file should be its own translation unit, not an include for main

hollow lynx
#

When I looked up opinions on Venza courses the results were mixed, but some people did say they were solid intro courses. Bleh

#

Would that be causing the inclusion error, or?

amber dawn
#

It's not how any proper C++ project is done.

amber dawn
hollow lynx
#

Like ultimately this is a project that doesn't mean a whole lot, but the hope was that at the very least it gave me some structure to reference and understand classes and running something basic like this. So it would be nice if, garbage code or not as a whole, I could make this silly project run before I completely toss it and hopefully find a better course

amber dawn
#

It's going to take some effort to transform this mess of a project structure into a proper one.

#

But first I would advise you get Visual studio for this.

hollow lynx
#

What's the difference and why is that preferred? I haven't had any complaints with the program so far

#

And "proper" is probably not the end goal here. "Runs" is, haha

amber dawn
#

Visual studio is a fully featured development environment with everything already set up for you.

amber dawn
hollow lynx
#

Yeah I mean I get that. Just in the short term it would be nice to understand what the inclusion error is and how to avoid it in the future, structure of everything aside

And could you throw me a link of what I should download instead?

amber dawn
#

Unfortunately M$ misleadingly markets completely different products on the same website. The proper thing is the purple one, while you are using the blue which is inferior for this purpose.

hollow lynx
#

Alright, I guess I'll grab "Community" since I'd assume that's the free one

amber dawn
#

Yes.

#

You can read this to get started:

tame crescentBOT
#
Getting Started With Visual Studio

New here and looking to quickly get started with C or C++ on Windows? Follow the steps in this thread to compile and run your first bits of code in Visual Studio.

elder lava
#

What is happening is that because #include are nothing more than textual includes, and the chain of include soup with .cpp files from main, you end up with one long preprocessed source file that has the same classes defined multiple times

#

You probably need to feed each cpp file into the compiler via the build command and include the .h files rather than the .cpp files

amber dawn
#

By using Visual studio you won't have to worry about this "feeding" process.

hollow lynx
#

I am currently installing Visual Studio, though

amber dawn
#

Visual studio will take care of the "build commands" for you. Then we are going to just focus on making a proper project structure.

hollow lynx
#

In the end I will definitely look for a more comprehensive beginner's "build a text game" tutorial that treats me like I'm five. It would just be a little demotivating to spend a week going through a crash course on the basics and this silly project to walk away from it with something that doesn't run and being told "course sucks, instructor sucks, program is wrong, start from scratch somewhere else" haha. Though I don't deny all of those things are probably completely true, I have motivation issues for sure

elder lava
#

player.cpp, item.cpp and gameChar.cpp all compile fine on their own, room.cpp has a capitalization error (Room.h instead of room.h), but after that also compiles fine. dungeon.cpp has a shitload of circular dependencies

hollow lynx
#

I don't imagine main.cpp compiles for you, though?

#

And yeah, I had a couple of those capitalization errors because I'm a dumbass. Missed that one

elder lava
#

No, main far from compiles

hollow lynx
#

Okay haha

#

This specific bit in room.h was strange in the course

#

The commented out part is how the course initially had me write it

#

But then at some point he hit an error

#

And said he had to define "defaults" or something

amber dawn
#

That's just straight up poor code not gonna lie.

hollow lynx
#

At which point he went back here and added the = bits for the vectors and everything (int and bool as well)

#

But then that change threw errors for me in dungeon.cpp when I referenced rooms

dense nexus
hollow lynx
#

So I have no clue

dense nexus
#

Just make a bloody default constructor, this course is horrid ngl

hollow lynx
#

I'm sure it sucks, just hearing it sucks doesn't especially help lol

#

What would be preferable? Something to reference?

amber dawn
hollow lynx
#

Well

elder lava
#

Okay it's not super hard to get it working..

in dungeon.cpp remove #include "player.h" and in main.cpp remove all the cpp includes except dungeon, but turn that into dungeon.h

#

$ g++ -o test main.cpp dungeon.cpp gameChar.cpp item.cpp player.cpp room.cpp
$ ./test
It feels like you've been sleeping for a long time, but something is lulling you back to consciousness. Who are you?

hollow lynx
#

Undefined references, I guess

elder lava
#

You gotta pass all the .cpp files to the compiler, not just main.cpp

#

like this line g++ -o test main.cpp dungeon.cpp gameChar.cpp item.cpp player.cpp room.cpp

hollow lynx
#

Oop. So is that why he was including all the cpps? As a weird lazy shortcut to doing that?

elder lava
#

Not a shortcut, it won't work that way. The code they presented will literally never work, everything end up getting defined multiple times

hollow lynx
#

So I have to insert this bit somewhere, you mean? $ g++ -o test main.cpp dungeon.cpp gameChar.cpp item.cpp player.cpp room.cpp $ ./test

#

Where would I do that?

elder lava
#

like in this bit

#

You are only using "main.cpp'

#

If thats just some "run" command from vscode, then you need to modify the build script or whatever

hollow lynx
#

Okay, right. Yeah I am in VSC and wasn't taught anything about build scripts in these two courses

amber dawn
#

Well have you done installing VS?

hollow lynx
#

Getting there. Incredibly slow install on this potato

elder lava
#

In vscode, do you have a .vscode folder in your project folder with a tasks.json in it?

dense nexus
elder lava
#

I assume that's how its only feeding main.cpp into the compiler, my default tasks.json gets something like

            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
hollow lynx
#

I didn't have a task file, or didn't know where it was, but now I do

elder lava
#

So its just trying to run a single file, which doesn't make a whole lot of sense in context of most c++ projects

#

It will work with a single file hello world project, but anything more complex with multiple .cpp files it wont work

hollow lynx
#

Imagine a course spending a few minutes explaining this part in correlation to having you set up a project with multiple cpps... wild thought

hollow lynx
elder lava
#

You could just replace the "${file}" with "main.cpp dungeon.cpp gameChar.cpp item.cpp player.cpp room.cpp"

#

Actually doesnt seem like that works

hollow lynx
#

Yeah, doesn't seem to

amber dawn
#

It should work if the source files are written properly.

#

Currently they are far from that.

elder lava
#

I think you might have to do each file in its own quotes

hollow lynx
#

I mean it's the same output, undefined, not pulling from the other cpps besides main

#

I'll try that

amber dawn
hollow lynx
#

Same outcome

#

With this tasks file { "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\ucrt64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "main.cpp", "dungeon.cpp", "gameChar.cpp", "item.cpp", "player.cpp", "room.cpp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "compiler: C:\\msys64\\ucrt64\\bin\\g++.exe" } ]

#

Is the label creating an issue?

elder lava
#

Works like that for me, but you gotta use the run command with a cpp file open, not the json

#

else its gonna try to run the json

hollow lynx
#

Yeah, I just still get the same result. Hm

amber dawn
#

The compiler command is unchanged, meaning you are probably not editing the correct configuration.

#

Maybe just wait until VS finishes installing and get over this headache.

elder lava
#

That sounds like a lovely idea

hollow lynx
#

Bleh, alright

#

I just used the ctrl+shift+p Configure Task thing Natashi suggested

elder lava
#

You don't have to use tasks to build it though, you can just drop g++ main.cpp dungeon.cpp gameChar.cpp item.cpp player.cpp room.cpp -o main in the terminal for the time being and it will create the main executable in that folder

#

Then ./main to run it

hollow lynx
#

Ah, I see

#

Okay, well I've got Visual Studio running now. I opened the project folder and have this now

amber dawn
#

Close it. Create a new project.

hollow lynx
#

That said, VSC had me mess with a few settings to get started up, like allowing input in terminal, choosing g++, etc

#

I did that first. How should I import my stuff instead?

amber dawn
#

Copy your source files into the project folder, then drag .cpp files onto Source files and .h files onto Header files.

tame crescentBOT
#

@hollow lynx Has your question been resolved? If so, type !solved :)

hollow lynx
#

Should I be seeing the files I copied into the folder here?

#

All good, just thought they would show up there first. They're where they should be now just dragging from explorer

amber dawn
#

No, you need to manually add them, that's what the dragging is for.

#

(This is objectively shit behavior though.)

#

So if you have the source files in place now you can literally try pressing the debug button.

#

That automatically runs the compilation process if needed.

hollow lynx
#

Huh, okay. Lots of things to look at, but it builds just fine now. Immediately running into issues, but those are just the wrong bits and pieces triggering when they shouldn't, which is likely my own errors

amber dawn
#

See, that's a much easier process. You don't even have to wrap your mind around any of it. :)

hollow lynx
#

And the error was me being very silly with a lazy copypaste job on room class references all being the same value. Game works beginning to with seemingly no issues. Phew

#

!solved

tame crescentBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

hollow lynx
#

Thanks for all the assistance @amber dawn and @elder lava. Will take a bit of getting used to VS as opposed to VSC (I miss how VSC assigns different colors to {} brackets to visualize things), but seems like the more worthwhile option

amber dawn
#

VS probably has an extension for that.

hollow lynx
#

Bonus round: This course has a number of questionnaire segments that ask you questions the course never even remotely touches on or mentions. A very strange method of teaching, but okay haha

dense nexus
#

That sure seems like something that's totally absolutely necessary to know as a C++ developer

hollow lynx
#

It also reuses the same exact questions 2-3 times each between questionnaires

#

And then throws in random trivia-esque ones like that

dense nexus
#

I sincerely very much hope you didn't pay anything for this course

hollow lynx
#

I did not

#

But apparently their own website and whatnot, Venza Academy, is a paid thing

#

This was a course on Alison

#

Which has numerous different companies and educators and whatnot for random subjects

#

So it's probably meant to be starter stuff that then encourages someone to go to their own site for more advanced courses

dense nexus
#

That doesn't make it any better

#

In fact arguably worse

hollow lynx
#

No argument here

dense nexus
#

You shouldn't be teaching beginners bad coding practices

hollow lynx
#

It's tough knowing where is the best bet to start out when you're a total newbie. I'm doing courses to count towards work hours with some state agency stuff, and I was referred to Alison for that. Which seems fine in and of itself, but obviously there are decent courses and terrible ones

#

So when I found the C++ courses, I looked up Venza and found mixed opinions