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.
109 messages · Page 1 of 1 (latest)
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.
@coral nova
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
Do yourself a favor and make a function that can take a vector or array as a parameter, and flip the visibility of each item
Then you can reduce all of that down to like two lines of code
that is a very good hint. i will we write the code to loops
Do any of those functions work or does it have the same error for all of them?
no its just this part:
the OnClick code goes like this:
if (Defense.isMouseOver(window) && Defense.onlcick_name == "Defense") {
WallSmallMk1.setVisibility(!WallSmallMk1);
WallSmallMk2.setVisibility(!WallSmallMk2);
WallSmallMk3.setVisibility(!WallSmallMk3);
WallSmallMk4.setVisibility(!WallSmallMk4);
WallSmallMk5.setVisibility(!WallSmallMk5);
. . .
}
all functions work but in this case i have a problem
Are all of these objects of the same type or no?
And if they're different, do all of these objects have the .setVisibility() method in their class?
yess
i solved the problem btw. in the create object file was "TurretSingleMK1" that causes the problem that "Turret Mk1" doestn exist
Ah I see, nice
can you help me to set up loops ?
I can give you an idea of what it might look like, sure
Thx
void invertVisibility(std::vector<type> &vector) {
for (auto &item : vector) {
item.setVisibility(!item);
}
}
Something like that
where type is the object type
And then you make a vector of the objects you want to flip, call that function, and pass it in
Ok i will try
Ok now i have to code a loop which creates the button ....
Thats easy ...
i think
Can you please help me to set up the hole script ?
I have many button which includes "Mk". I want a loop which creates all btns which have "Mk" in it.
There are 5 type: Mk1, Mk2, Mk3, Mk4
There are different objects: Wall, Gate, Turret, ....
I want to build a loop which constucs all the diferent btn objects and types. My provlem is that i dont know how to begin
I would just create those manually, unless you need to create hundreds for example. If that's the case there's probably a better way to do it
But for now just create them manually, add them all to a vector (you can use std::push_back(), google if it need be), and then pass that vector into the function
If I do all the work for you you wouldn't learn anything, I would implore you to do what you can on your own and give it a try, and then ask again if you run into issues 🙂
so you say that a loop which creates all the Mk versionen of the objects is stupid ?
I dont know whats incorrect here:
What does the error say?
I'll give you a hint, check the type of the vector compared to the type of the object you're trying to insert into it
This declaration has no storage class or type specifier.
i dont know which type my buttons are. think they are object class element or something like that. but i dont know hot to setup the vector correctly
You are trying to put a Button in a std::vector<std::string>, where std::string is the data type that the vector should hold
The Button WallSmallMk1 = … means that the type of the variables is Button (wherever that’s defined)
This declaration has no storage class or type specifier.
thats the error when I hover "buildDefense.push_back()"
Can you post the line verbatim
std::vector<Button> buildDefense;
Button WallSmallMk1 = Button("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false, wallsmallmk1Color);
Button WallSmallMk2 = Button("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false, wallsmallmk2Color);
Button WallSmallMk3 = Button("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false, wallsmallmk3Color);
Button WallSmallMk4 = Button("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false, wallsmallmk4Color);
Button WallSmallMk5 = Button("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false, wallsmallmk5Color);
buildDefense.push_back(WallSmallMk1);
============
Like that ?
!f
Nothing to format
std::vector<Button> buildDefense;
Button WallSmallMk1 = Button("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false, wallsmallmk1Color);
Button WallSmallMk2 = Button("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false, wallsmallmk2Color);
Button WallSmallMk3 = Button("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false, wallsmallmk3Color);
Button WallSmallMk4 = Button("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false, wallsmallmk4Color);
Button WallSmallMk5 = Button("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false, wallsmallmk5Color);
buildDefense.push_back(WallSmallMk1);
You likely have the copy constructor deleted for Button, and .push_back performs a copy, which will fail if the copy constructor is deleted.
What you likely need is .emplace (even if it makes error messages somewhat gnarly if used incorrectly):
std::vector<Button> buildDefense;
buildDefense.emplace("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false, wallsmallmk1Color);
buildDefense.emplace("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false, wallsmallmk2Color);
buildDefense.emplace("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false, wallsmallmk3Color);
buildDefense.emplace("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false, wallsmallmk4Color);
buildDefense.emplace("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false, wallsmallmk5Color);
I just realized you need .emplace_back, emplace needs an additional "where" parameter.
So the code would actually be:
std::vector<Button> buildDefense;
buildDefense.emplace_back("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false, wallsmallmk1Color);
buildDefense.emplace_back("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false, wallsmallmk2Color);
buildDefense.emplace_back("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false, wallsmallmk3Color);
buildDefense.emplace_back("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false, wallsmallmk4Color);
buildDefense.emplace_back("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false, wallsmallmk5Color);
;compile cpp
#include <string>
#include <vector>
class Button {
public:
Button(std::string a, std::string b, int c, int d, int e, int f, std::string g, int h, bool i) {};
};
int main(){
std::vector<Button> buildDefense;
buildDefense.emplace_back("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false);
buildDefense.emplace_back("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false);
buildDefense.emplace_back("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false);
buildDefense.emplace_back("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false);
buildDefense.emplace_back("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false);
}
No output.
I tried this code lines. but even with .emplace_back the error is here
"This declaration has no storage class or type specifier."
there is something wrong with the vector definition
Did you add #include <vector> at the begining of the file?
yes
Then your Button's move constructor is likely deleted as well, which means that you need to use pointers, I would strongly suggest smart pointers, like std::unique_ptr:
#include <string>
#include <vector>
#include <memory>
class Button {
public:
Button(std::string a, std::string b, int c, int d, int e, int f, std::string g, int h, bool i) {};
// Delete the copy constructors, to make this behave as the OG Button likely does
Button (const Button&) = delete;
Button& operator= (const Button&) = delete;
};
int main(){
std::vector<std::unique_ptr<Button>> buildDefense;
buildDefense.emplace_back(std::make_unique<Button>("WallSmallMk1", "WallSmallMk1", 440, 25, 120, 25, "Wall Small Mk1", 15, false));
buildDefense.emplace_back(std::make_unique<Button>("WallSmallMk2", "WallSmallMk2", 440, 50, 120, 25, "Wall Small Mk2", 15, false));
buildDefense.emplace_back(std::make_unique<Button>("WallSmallMk3", "WallSmallMk3", 440, 75, 120, 25, "Wall Small Mk3", 15, false));
buildDefense.emplace_back(std::make_unique<Button>("WallSmallMk4", "WallSmallMk4", 440, 100, 120, 25, "Wall Small Mk4", 15, false));
buildDefense.emplace_back(std::make_unique<Button>("WallSmallMk5", "WallSmallMk5", 440, 125, 120, 25, "Wall Small MK5", 15, false));
}
;compile
No output.
im unsure how to change the class "Button" to use this methode
You wouldn't need to change Button, I just changed my defenition to delete the copy and move constructors, and make it more in-line with what you are seeing in your Button.
The std::unique_ptr handles the copy/move as only it is moved, not the Button it's pointing to
std::vector has a limitation that it needs to move objects in the underlying array if it runs out of space, thus a move constructor is needed
i chnage to line to:
std::vector<std::unique_ptr<Button>> buildDefense;
nothing changed at the "buildDefense.emplace_back" but the error ist thill there. I also wrote "#include<memory>"
The easiest way to construct std::unique_ptr is to call the std::make_unique function, as I did in the emplace_back (in case you overlooked that, since it is in the midst of the code)
i copied your sctipt lines into my file. but that doesnt change the error message
Can you try running a full compile, in case you aren't, since sometimes IDEs cache error messages
done
i also read the error messages
it seems that the compiler tries to convert "Button" into a bool
that one of the error
You probably need to share some more code in that case, since it seems that is rather weird behavior for the compiler to attempt
Also, what gui library are you using?
SFML
currently im trying to correct the errors from the default code:
in this case i dont youse a vector to storage al the components.
With this setting I only have one error:
"onclick_name" is not part of the obj.
when the compiling of this methode works and the .exe file starts without any problems then i will go back and try to include the vertors
no way, the compiling was succssesful
but the .exe file closes itselfe after starting it
when the programm works without any problems, i will trie to include the vectors 🙂
how do i fix this problem ?
There are a thousand reasons it could be,
Did you run it from the command line (in case an error code like segfault)?
no, i double cliked the exe file
Try running from CMD/Powershell, since that gives you an error message when your program exits with a fault
what the cmd command to run a exe file ?
.\<relative path to exe>
i did already
Also I just noticed, since you're in VS, you can run using the Run/Debug button, this gives even better error info, since you can step through using the debugger
done
I will copy the errors: cause i dont know what the errors mean:
1>ConsoleApplication1.obj : error LNK2019: Reference to unresolved external symbol ""__declspec(dllimport) public: __cdecl sf::Font::~Font(void)" (_imp??1Font@sf@@QEAA@XZ)" in function ""public: __cdecl Window::~Window(void)" (??1Window@@QEAA@XZ)".
1>ConsoleApplication1.obj : error LNK2019: Reference to unresolved external symbol ""__declspec(dllimport) public: virtual __cdecl sf::RenderWindow::~RenderWindow(void)" (_imp??1RenderWindow@sf@@UEAA@XZ)" in function ""public: __cdecl Window::~Window(void)" (??1Window@@QEAA@XZ)".
1>ConsoleApplication1.obj : error LNK2019: Reference to unresolved external symbol ""public: __cdecl Window::Window(void)" (??0Window@@QEAA@XZ)" in function "main".
1>ConsoleApplication1.obj : error LNK2019: Reference to unresolved external symbol ""public: void __cdecl Window::create_objects(void)" (?create_objects@Window@@QEAAXXZ)" in function "main".
1>C:\Users\UsersIn\Documents\Visual Studio 2022\Projects\ConsoleApp1\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe : fatal error LNK1120: 4 unresolved externals
1>The creation of the project "ConsoleApplication1.vcxproj" is complete -- ERROR.
========== Build: 0 successful, 1 error, 0 current, 0 skipped ==========
========== Build completed at 10:06 and took 02:52,846 minutes ==========
You likely forgot to add the dll for it to link to (which includes SFML)
@coral nova did you try turning off your pc
no
Then look no further
Also, since I have to go, if it's still not running:
how do I dow that ?
Also I see you’re using visual studio as your IDE I would recommend switching to Repl.it to get the full coding experience
You can follow the SFML docs: https://www.sfml-dev.org/tutorials/2.6/start-vc.php
i dont want to. setting vs up ind include cmake was very confusing
if you mean that I should add SFMl to the environment variables of windows, then I already did that the day before yesterday
If you're using the templates/cmake, this should be done automatically, and the built libraries are under build/_deps/SFML-build/library
The main point is to add it to the Additional Library Directories, which it links to (the step that's currently failing for you)
and i set this in the CMakeLists.txt file, right?
Usually yes, though it's apparently failing to find the symbols, so there may be something wrong with how you linked it
i dont see a mistake in the CMakeLists.txt
Well, it's not finding the symbols, you can scroll through https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2019?view=msvc-170 to get a comprehensive list of possible causes, though it's likely you have an error in the cmake, or you are using the wrong paths to link
Sorry I didn’t realize roles where switched up I thought you were trolling since I saw you were an expert ðŸ˜ðŸ˜
i need a break from coding O.o
!solved
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