M
M
MrZed2021-08-16 00:16:35
C++ / C#
MrZed, 2021-08-16 00:16:35

C++ System executing commands with arguments?

Hey! I am writing a program (if I may say so) a C++ compiler .
THE CODE:

#include <iostream>
#include <string>

using namespace std;

int main () {
  string test = "test.cpp";
  string command = "cl /EHsc " + test;

  system(command);
  return 0;
}

I am getting an error:
main.cpp(10): error C2664: "int system(const char *)": невозможно преобразовать аргумент 1 из "std::basic_string<char,std::char_traits<char>,std::allocator<char>>" в "const char *"
C:\Users\User\Desktop\main.cpp(10): note: Для выполнения данного преобразования нет доступного оператора преобразования, определенного пользователем, или вызов оператора невозможен

Sorry if this is a dumb question, I've only been learning C++ for 2 days.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cunion, 2021-08-16
@MrZed

The problem is that you are trying to concatenate with . Also in the function that accepts you put a line from std. You need to call y to get a C-style stringconst char*std::stringconst char*c_strstd::string

int main(int argc, char** argv)
{
  std::string test = "test.cpp";
  std::string command = "cl /EHsc " + test;

  system(command.c_str());
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question