C++ Xmemory error


#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>


using namespace std;


class Main

{

public:
static void main(std::vector<std::wstring>& args);

static void fun(std::vector<int>& arr, int n);

};


#include <string>
#include <vector>

int main(int argc, char** argv)
{
std::vector<std::wstring> args(argv + 1, argv + argc);
Main::main(args);
}



void Main::main(std::vector<std::wstring>& args)

{

try
{

// declare and initialize array

std::vector<int> arr1 = { 1, 5, 10, -2, 4, -3, 6 };

int num1 = 3;

std::wcout << L"Numbers greater than " << num1 << L" in the 1st array are :" << std::endl;

// call the function to print the Number greater than 3

fun(arr1, num1);

// declare and initialize array

std::vector<int> arr2 = { 10, 7, 12, 17, 22 };

int num2 = 12;

std::wcout << L"Numbers greater than " << num2 << L" in the 2nd array are :" << std::endl;

// call the function to print the Number greater than 12

fun(arr2, num2);

}
catch (const std::runtime_error)
{

return;
}

}

void Main::fun(std::vector<int>& arr, int n)

{

for (int i = 0; i < arr.size(); i++)

{

// if the array element is greater than the value of n, then print it

if (arr[i] > n)
{

std::wcout << arr[i] << L" ";
}

}

std::wcout << std::endl;

}

Severity Code Description Project File Line Suppression State
Error C2664 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>::basic_string(std::initializer_list<_Elem>,const _Alloc &)': cannot convert argument 1 from 'char *' to 'std::initializer_list<_Elem>' Programming Project2 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory 714



I am unsure exaclty where the error is or how to fix it has its in the xmemory. Not sure how to adjust argument 1 to get the code to run. Any help is appreciated.
Last edited on
I don't see it, but its telling you that you have bad arguments to a constructor; I think its
std::vector<std::wstring> args(argv + 1, argv + argc);

if you look at your error, it will have the line number (and column) where it choked.
Its not in xmemory, that is a symptom, but one of your error messages will point to your file and a line number.

it is unusual to wrap main in an object without reason. If you have a reason, its fine. If not, its 'java like' and 'weird' in c++.
Last edited on
Problem is that std::wstring, a string of wchar_t, can't be constructed using a pointer to char.

To fix the problem,
Replace every wcout with cout
Replace every wstring with string
Replace every L" with "

Then fix up, removing code that doesn't do anything:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <iostream>

void print_elements_greater_than(std::vector<int> const& xs, int n)
{ 
  for (int x: xs) 
    if (x > n) std::cout << x << ' ';
  std::cout << '\n';
}

int main()
{
  std::vector<int> const arr1 { 1, 5, 10, -2, 4, -3, 6 };  
  int const num1 = 3;
  std::cout << "Numbers greater than " << num1 << " in the 1st array are :\n";
  print_elements_greater_than(arr1, num1);
  
  std::vector<int> const arr2 { 10, 7, 12, 17, 22 };
  int const num2 = 12;
  std::cout << "Numbers greater than " << num2 << " in the 2nd array are :\n";
  print_elements_greater_than(arr2, num2);
}

Last edited on
I
Last edited on
Or what should I use instead of main?
mbozzi's code above works for me with VS2022. What problems are you having with it?
It's telling me it's in line 714 which doesn't exist for me to view.

This is an artifact of how the C++ standard library operates. Symptoms of the error appear in the implementation of the standard library rather than your code. Line 714, of a different file.

Every mainstream compiler follows the source of the error back to your code. Keep reading the error message, always top-down, until the first time it refers to a file and line number in your code. Start there.

Generally, there is no point in trying to diagnose an error by looking at the implementation of the standard library (e.g. in xmemory).
Last edited on
Or what should I use instead of main?


are you asking me? Most code looks like Mbozzi's above, a main function that creates some variables and kicks off another method (whether belonging to an object or not).
the extra object for "Main" was just visual clutter -- its not "wrong" but served no purpose in c++.
Topic archived. No new replies allowed.