i have 1 class Multithread, but seems that i have some errors:(

#include <process.h>
#include <string>

using namespace std;

class MultiThread
{
    private:
        void * b;
    public:
        void ParameterList(void *c)
        {
            b=c;
        }

        void Thread(unsigned (__stdcall*) (void *)  )
        {
            _beginthreadex(NULL, 0, (__stdcall*) (void *)  , b, 0, NULL);
        }
};

the parameterlist is a struture for the thread arguments.

error message:
"error: expected primary-expression before 'attribute'"
"error: expected ')' before 'attribute'"
can anyone advice me?

Recommended Answers

All 19 Replies

Line 16, the parameter to Thread is not named.

Line 18, third parameter to _beginthreadex consists of '(__stdcall*) (void *)' which is not a valid anything, perhaps it was meant to be the unnamed function parameter?

the best is show you all the file:

#include <process.h>
#include <string>


using namespace std;

class MultiThread
{
    private:
        void *b;
    public:
        void ParameterList(void *c)
        {
            b=c;
        }

        void Thread(unsigned (__stdcall*) (void *)  )
        {
            _beginthreadex(NULL, 0, (__stdcall*) (void *)  , &b, 0, NULL);
        }
};

void SetCaretPosition (int x, int y)
 {
     COORD coord; // coordinates
     coord.X = x; coord.Y = y; // X and Y coordinates
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
 }

COORD GetCaretPosition()
{
    COORD s;
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    s.X=csbi.dwCursorPosition.X;
    s.Y=csbi.dwCursorPosition.Y;
    return s;
}

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(string Text, int x, int y)
{
    char *EmptyText;
    EmptyText=new char[ Text.length() ] ;
    int i;
    for( i=0;i<=Text.length();i++)
    {
        EmptyText[i] = ' ';
    }
    EmptyText[i] ='\0';
    HDC a=GetDC(GetForegroundWindow());
    while (true)
    {
        TextOut(a,x,y,Text.c_str(),Text.length);
        Sleep(1000);
        TextOut(a,y,x,EmptyText,Text.length);
        Sleep(500);
    }
    return 0;
}

void TextBlink(string Text, int x, int y, int TextColor, int BackColor)
{
    MultiThread blink;
    Blink b;
    b.Text=Text;
    b.BackColor=BackColor;
    b.TextColor=TextColor;
    b.x=x;
    b.y =y;
    blink.ParameterList(&b);
    blink.Thread(&BlinkLoop);
}

seems that i have several errors on these file :(
"C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h||In member function 'void MultiThread::Thread(unsigned int (attribute((stdcall)) )(void))':|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|19|error: expected primary-expression before 'attribute'|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|19|error: expected ')' before 'attribute'|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h||In function 'unsigned int BlinkLoop(std::string, int, int)':|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|55|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|63|error: cannot convert 'std::basic_string<_CharT, _Traits, _Alloc>::length<char, std::char_traits<char>, std::allocator<char> >' from type 'std::basic_string<char>::size_type (std::basic_string<char>::)()const noexcept (true) {aka unsigned int (std::basic_string<char>::)()const noexcept (true)}' to type 'int'|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|65|error: cannot convert 'std::basic_string<_CharT, _Traits, _Alloc>::length<char, std::char_traits<char>, std::allocator<char> >' from type 'std::basic_string<char>::size_type (std::basic_string<char>::)()const noexcept (true) {aka unsigned int (std::basic_string<char>::)()const noexcept (true)}' to type 'int'|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h||In function 'void TextBlink(std::string, int, int, int, int)':|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|81|error: invalid conversion from 'unsigned int (attribute((stdcall)) *)(std::string, int, int) {aka unsigned int (attribute((stdcall)) *)(std::basic_string<char>, int, int)}' to 'unsigned int (attribute((stdcall)) *)(void*)' [-fpermissive]|
C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|17|error: initializing argument 1 of 'void MultiThread::Thread(unsigned int (attribute((stdcall)) *)(void*))' [-fpermissive]|
||=== Build finished: 6 errors, 1 warnings (0 minutes, 2 seconds) ===|"

I think what you should use is the std::thread class, see docs here. You would get the following:

#include <process.h>
#include <string>
#include <thread>

using namespace std;


void SetCaretPosition (int x, int y)
 {
     COORD coord; // coordinates
     coord.X = x; coord.Y = y; // X and Y coordinates
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
 }

COORD GetCaretPosition()
{
    COORD s;
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    s.X=csbi.dwCursorPosition.X;
    s.Y=csbi.dwCursorPosition.Y;
    return s;
}

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

void BlinkLoop(const Blink& b)
{
    char *EmptyText;
    EmptyText=new char[ b.Text.length() ] ;
    int i;
    for(i = 0; i <= b.Text.length(); i++)
    {
        EmptyText[i] = ' ';
    }
    EmptyText[i] ='\0';
    HDC a = GetDC(GetForegroundWindow());
    while (true)
    {
        TextOut(a, b.x, b.y, b.Text.c_str(), b.Text.length());
        Sleep(1000);
        TextOut(a, b.y, b.x, EmptyText, b.Text.length());
        Sleep(500);
    }
}

void TextBlink(string Text, int x, int y, int TextColor, int BackColor)
{
    Blink b;
    b.Text=Text;
    b.BackColor=BackColor;
    b.TextColor=TextColor;
    b.x=x;
    b.y =y;
    std::thread t(BlinkLoop, std::cref(b));
}

which will have exactly the desired effect of your original code.

If you can't use std::thread because your compiler is too old, then just use Boost.Thread library.

finally i fix several errors. now i just have 2 errors because of pointers:

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    char EmptyText[p->Text.length()+1];
    memset(EmptyText,' ', sizeof(EmptyText));
    EmptyText[ sizeof(EmptyText)-1 ] ='\0';
    HDC hDC=GetDC(GetForegroundWindow());
    while (true)
    {
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length());
        Sleep(1000);
        TextOut(hDC,p->y,p->x,EmptyText,(int)p->Text.length());
        Sleep(500);
    }
    return 0;
}

see the TextOut(). what i'm doing wrong?
error message:
"C:\Users\Joaquim\Documents\CodeBlocks\blinktext\blinktext.h|45|undefined reference to `_imp__TextOutA@20'|"

did you include windows.h?

yes

so you need see all the code?

#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>

using namespace std;

void SetCaretPosition (int x, int y)
 {
     COORD coord; // coordinates
     coord.X = x; coord.Y = y; // X and Y coordinates
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
 }

COORD GetCaretPosition()
{
    COORD s;
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    s.X=csbi.dwCursorPosition.X;
    s.Y=csbi.dwCursorPosition.Y;
    return s;
}

struct Blink
{
    string Text;
    int x;
    int y;
    int TextColor;
    int BackColor;
};

unsigned __stdcall BlinkLoop(void *params)
{
    Blink *p = static_cast<Blink *>(params);

    char EmptyText[p->Text.length()+1];
    memset(EmptyText,' ', sizeof(EmptyText));
    EmptyText[ sizeof(EmptyText)-1 ] ='\0';
    HDC hDC=GetDC(GetForegroundWindow());
    while (true)
    {
        TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length());
        Sleep(1000);
        TextOut(hDC,p->y,p->x,EmptyText,(int)p->Text.length());
        Sleep(500);
    }
    return 0;
}
void TextBlink(string Text, int x, int y, int TextColor, int BackColor)
{
    Blink *b=NULL;
    b->Text=Text;
    b->BackColor=BackColor;
    b->TextColor=TextColor;
    b->x=x;
    b->y =y;
    _beginthreadex(NULL, 0, BlinkLoop  , b, 0, NULL);
}

i belive that i know why the error: can i add the GDI32.a by code?
the:

#pragma comment(lib,"gdi32.dll")

is ignored:
"warning: ignoring #pragma comment [-Wunknown-pragmas]"
i'm using CodeBlocks

pragmas are not portable from one compiler to the other. Apparently Code::Blocks knows nothing about that pragma. CB has project settings for that purpose.

i add it on linker options, but the text wasn't showed

#include <iostream>
#include <string.h>
#include <windows.h>
//#include "blinktext.h"
//#pragma comment(lib,"gdi32.dll")
using namespace std;

int main()
{
    //TextBlink("hello world", 10,20,3,5);
    HDC hDC=GetDC(GetForegroundWindow());
    SetTextColor(hDC,5);
    TextOut(hDC,1,5,"hello world",11);
    cin.get();
}

This is a Visual C++ (or "MSVC", for short) extension. This line tells the compiler (if it can understand it) to link with the "gdi32.dll" library. The GCC compiler (MinGW, that Codeblocks uses) does not support this "automatic linking" feature. You must specify the library manually in your project configuration.

I don't know Codeblocks that well off the top of my head, so I can't guide you through the menus, but you should be able to find a place in the Linker configuration for adding additional libraries.

As for which library to link to, I believe that MinGW comes with an import library called libgdi32.a to link to the dynamic library gdi32.dll.

yes i did it. but the text isn't showed

#ifndef WINVER
#define WINVER 0x0501
#else
#if defined(_WIN32_WINNT) && (WINVER 0x0400)
#error WINVER setting conflicts with _WIN32_WINNT setting
#endif
#endif

do you know these code?
i don't know use it, but maybe can resolve it ;)
can you advice me?

Your first mistake was creating a console program -- you should have created a win32 program which uses WinMain() instead of main(). Here is a good tutorial to get you started.

i have 1 project for a console, on VSC++ and the function works fine.

I just ran your program with VC++ 2012 console and it did nothing.

i will do another thread, because i'm doing these wrong with forum :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.