[Error] no matching function

why got this error :[Error] no matching function for call to 'std::basic_string<wchar_t>::basic_string(TCHAR [260])'

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Windows.h"
#include <psapi.h> // For access to GetModuleFileNameEx
#include <iostream>
#include <string>


#ifdef _UNICODE
#define tcout wcout
#define tcerr wcerr
#else
#define tcout cout
#define tcerr cerr
#endif

HWND GetFocusGlobal()
{
    HWND Wnd;
    HWND Result = NULL;
    DWORD TId, PId;

    Result = GetFocus();
    if (!Result)
    {
        Wnd = GetForegroundWindow();
        if(Wnd)
        {
            TId = GetWindowThreadProcessId(Wnd, &PId);
            if (AttachThreadInput(GetCurrentThreadId(), TId, TRUE))
            {
                Result = GetFocus();
                AttachThreadInput(GetCurrentThreadId(), TId, FALSE);
            }            
        }
    }
    return Result;
}


int _tmain(int argc, _TCHAR* argv[])
{
    std::wstring state;

    while(1)
    {
        HWND focus_handle = GetFocusGlobal();

        if(focus_handle)
        {

            TCHAR text[MAX_PATH];
            GetClassName(focus_handle, text, MAX_PATH);

            const std::wstring cur_path(text);

            if(cur_path != state)
            {
                std::tcout << "new:" << focus_handle << " " << text << std::endl;
                state = cur_path;
            }

            }

        Sleep(50); // Sleep for 50 ms.
    }
}
Last edited on
Is _UNICODE defined?

That #define thing you do for wcout/cout, you need something like that for wstring/string.

The compiler's almost certainly reported exactly what's wrong, but you've only pasted a part of what it reported and are asking about the missing part.
Last edited on
I don't understand what to do to make the code work, I'm a beginner
and i copy this code from stackoverflow !
Can you post the full error please,

But you probably need to add:
1
2
3
4
5
6
7
8
9
#ifdef _UNICODE
#define tcout wcout
#define tcerr wcerr
#define tstring wstring
#else
#define tcout cout
#define tcerr cerr
#define tstring string
#endif 

Then use std::tstring instead of std::wstring
Last edited on
full error
1
2
3
39	22	D:\c+++++\main.cpp	[Error] '_TCHAR' has not been declared
D:\c+++++\main.cpp	In function 'int _tmain(int, int**)':
53	45	D:\c+++++\main.cpp	[Error] no matching function for call to 'std::basic_string<wchar_t>::basic_string(TCHAR [260])'


But you probably need to add:
#ifdef _UNICODE
#define tcout wcout
#define tcerr wcerr
#define tstring wstring
#else
#define tcout cout
#define tcerr cerr
#define tstring string
#endif

Then use std::tstring instead of std::wstring

i got error

1
2
3
47	22	D:\c+++++\main.cpp	[Error] '_TCHAR' has not been declared
D:\c+++++\main.cpp	In function 'int _tmain(int, int**)':
63	25	D:\c+++++\main.cpp	[Error] no match for 'operator!=' (operand types are 'const string {aka const std::basic_string<char>}' and 'std::wstring {aka std::basic_string<wchar_t>}')
You need to add:
 
#include tchar.h 


This'll explain what's going on: https://docs.microsoft.com/en-us/cpp/text/unicode-programming-summary?view=msvc-160
Last edited on
working thanks so much ,
Topic archived. No new replies allowed.