-2

I'm new to C++

Code:

#include <iostream> 
#include <string> 
#include <fstream>

#include <windows.h>


class spawnTools {
    
private:
    void specTools(std::string toolname) {

    }
    void normTools(std::string toolname) {

    }
public:
    std::string toolList =
    { "regedit", "cmd" };
    std::string toolListAdmin =
    { "regedit", "cmd" };
    void initToolSet(int xadmin) {
        if (xadmin == TRUE) {

        }
        else if (xadmin == FALSE) {

        }
        else {
            MessageBox(NULL, L"SYSTEM ERROR: Incorrect input into xadmin", L"Error", MB_OK | MB_ICONERROR);
        }
    }
};

int main() {
    spawnTools st;
    st.initToolSet(TRUE); <----- Exception thrown here
}

I have set up a class with 2 public strings and 1 public function. The private functions will be filled in later in the development (just so you know I'm not hiding any code).

I'm getting an Debug Assertion error that says I have a transposed pointer range; it's not like this question as I'm using std::strings, not a vector char.

And anyway the exception is thrown on the usage of the public function, not the std::strings.

I have tried using ' commas on the std::strings instead of the normal " commas, but that throws another error, so that doesn't work.

I have tried using struct in case it's to do with class. No luck.

clouded.
  • 15
  • 6
  • What do you do in `initToolSet` when `xadmin == TRUE`? Have you tried debugging it? – rturrado Dec 26 '22 at 22:44
  • 2
    `std::string toolList = { "regedit", "cmd" };` doesn't do what you want it to do. It looks like you're trying to pack two strings into a single `string`. – user4581301 Dec 26 '22 at 22:45
  • Note that you declare e.g. `toolList ` not to be a list of strings, but a *single* `string`. That together with other parts in your code leads me to believe you don't have a very good beginners book. So please invest [in some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn more of the basics first. – Some programmer dude Dec 26 '22 at 22:47
  • @rturrado if xadmin = TRUE I will use the admin version of tools instead of the normal version. and I have tried debugging it. – clouded. Dec 26 '22 at 22:48
  • @user4581301 I'm a noob sorry. I don't know what the correct type of string to use for an array is. – clouded. Dec 26 '22 at 22:49
  • @Someprogrammerdude I same as above. I'm a noob – clouded. Dec 26 '22 at 22:50
  • `struct` and `class` are the same except for the default access of `public` and `private` respectively. – Retired Ninja Dec 26 '22 at 22:51
  • And that why I really recommend trying to invest in some good books, like the ones I linked to earlier. It will be invaluable for learning "proper" C++, instead of what looks like "C with classes" that you're currently learning. In the long run it will be worth the money you invest in those books, and if you're serious about a future as a C++ programmer it's almost mandatory to own or at least have read a few of those books. I'm not trying to put you down or anything, but C++ is a very hard language to learn, but it's also very fun once you know it. :) – Some programmer dude Dec 27 '22 at 02:36

1 Answers1

2

You cannot initialize a std::string with two strings that way. The C strings "regedit" and "cmd" are being passed to a constructor that is normally used to pass start and end iterators of the string you want to initialize it with. This results in it attempting to initialize the string with the the address of "regedit" as the start iterator (address) of the string and the address of "cmd" as the end iterator (address) of the string. The assert you are getting is because the address of "cmd" is lower than "regedit".

Chances are you're going to want a std::array<std::string, 2> or std::vector<std::string> or even a naked std::string[2] array to hold them.

#include <string> 
#include <array>
#include <windows.h>

class spawnTools
{
private:
    void specTools(std::string toolname) {}
    void normTools(std::string toolname) {}

public:

    std::array<std::string, 2> toolList{ "regedit", "cmd" };
    std::array<std::string, 2> toolListAdmin{ "regedit", "cmd" };

    void initToolSet(int xadmin) {
        if (xadmin == TRUE) {}
        else if (xadmin == FALSE) {}
        else {
            MessageBox(
                NULL,
                L"SYSTEM ERROR: Incorrect input into xadmin",
                L"Error",
                MB_OK | MB_ICONERROR);
        }
    }
};

int main() {
    spawnTools st;
    st.initToolSet(TRUE);
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74