0

Alright, I am aware of my issue at the moment. I am trying to convert a string to a TCHAR. The error I get looks like this:

IntelliSense: initialization with '{...}' expected for aggregate object

The code I am using, large in part is from an msdn guide/tutorial where I am looking to replace lines about filling in a username and password based on briefly stored information. The guide asks for a prompt for username and password, however due to the requirements from my higher-ups, I can't do this. This C++ code is being ran as part of a larger C# program to add to the Microsoft Task Scheduler as C# doesn't have the functionality (as far as I can see).

string functionName; std::wstring functionNameW; LPCWSTR functionNameR; 

[insert irrelevant code]

ifstream myfile ("~~string location~~"); if (myfile.is_open()) { ... getline (myfile,functionName); functionNameW = s2ws(functionName); functionNameR = functionNameW.c_str(); //TCHAR of name TCHAR *paramName=new TCHAR[functionName.size()+1]; paramName[functionName.size()]=0; //As much as we'd love to, we can't use memcpy() because //sizeof(TCHAR)==sizeof(char) may not be true: std::copy(functionName.begin(),functionName.end(),paramName); } 

[insert irrelevant code]

TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName; //apparently not keep "const"-ness 

So basically I read in the desired name from a .txt file. I then convert this name into multiple string types that are required at various stages throughout my code.

Now I have read up on this error from other stack-overflow submissions, but I run into the problem that many of these threads are specifically about arrays/vectors AND there do not seem to be any answers on how to actually deal with this problem. Everyone merely states that this error is attempting to say you are putting a "const" value into a non-const variable (or vice-versa).

So, is there a way I can get through this error? If it is an error dealing with "const"-ness, can I de-const a variable when placing it in a new location?

If you're looking for a "full" mini-version of the code, I guess it would look like this:

#include "stdafx.h" //created when MVS made this #include <fstream> //need to read #include <string> //need to read #define _WIN32_DCOM //from msdn Task Manager Demo #include <windows.h> //from msdn Task Manager Demo #include <iostream> //from msdn Task Manager Demo #include <stdio.h> //from msdn Task Manager Demo #include <comdef.h> //from msdn Task Manager Demo #include <wincred.h> //from msdn Task Manager Demo // Include the task header file. //from msdn Task Manager Demo #include <taskschd.h> //from msdn Task Manager Demo # pragma comment(lib, "taskschd.lib") //from msdn Task Manager Demo # pragma comment(lib, "comsupp.lib") //from msdn Task Manager Demo # pragma comment(lib, "credui.lib") //from msdn Task Manager Demo using namespace std; std::wstring s2ws(const std::string& s) { int len; int slength = (int)s.length() + 1; len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); wchar_t* buf = new wchar_t[len]; MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len); std::wstring r(buf); delete[] buf; return r; } int __cdecl wmain() { string functionName; std::wstring functionNameW; LPCWSTR functionNameR; ifstream myfile ("~~string location~~"); if (myfile.is_open()) { getline (myfile,functionName); functionNameW = s2ws(functionName); functionNameR = functionNameW.c_str(); //TCHAR of name TCHAR *paramName=new TCHAR[functionName.size()+1]; paramName[functionName.size()]=0; //As much as we'd love to, we can't use memcpy() because //sizeof(TCHAR)==sizeof(char) may not be true: std::copy(functionName.begin(),functionName.end(),paramName); } TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName; //apparently not keep "const"-ness } 
12
  • What is paramUserName? Please present a real testcase. Commented May 28, 2015 at 19:54
  • @LightnessRacesinOrbit apologies, it was meant to be 'paramName'. This is the smallest amount of code I can trim down to that creates the same problem. It is just a means of converting from C++ String to C++ TCHAR. Commented May 28, 2015 at 19:59
  • I can guarantee you can trim it more! But the testcase must be complete, which this isn't. Please follow the link I gave you (you were also presented with this information when you joined). Commented May 28, 2015 at 20:00
  • 1
    What would you expect TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = paramName; to do? Copy the string? If you want to copy the data then you should do that with _tcscpy or whatever. Your short example doesn't compile because paramName isn't in scope, but if you correct that and try to compile you'll see the error. Commented May 28, 2015 at 20:04
  • @LightnessRacesinOrbit I believe I added at complete version to the bottom of the original post. If it is not as trimmed as possible I am not sure where I can trim it. C++ is somewhat foreign to me, especially in this type of use. I am currently on internship and the only person working on this project outside of the scope of what I've learned previously so I am learning as I go and have hit this wall. Commented May 28, 2015 at 20:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.