Hi.. can anyone tell me what is the meaning of this error??

[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'char' for argument '1' to 'char strcpy(char*, const char*)'

When I'm taking TeamName as char TeamName[30]; , the compiler shows linker error.
what could be the problem?
I use DEV C++ 5.2.0.3

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
class team
{
    team *base;
    string TeamName;

    public:
        team()
        {
          base = NULL;  
        }
        void create_team();
        void front();
};


void team::create_team()
{
    string x;
    team *temp = new team;
    cout<<"ENTER THE TEAM NAME: ";
    cin>>x;
    strcpy(temp->TeamName,x);       //FACING ERROR HERE!!        
}

Dani AI

Generated

The error happens because strcpy works with C-style character buffers, not std::string objects. In the posted snippet TeamName is a std::string (and x is too), so calling strcpy on them is a type mismatch and unsafe. Prefer using std::string operations or, if you must copy into a fixed-size char[], copy from the std::string's c_str() with bounds checking:

char buf[30];
std::strncpy(buf, x.c_str(), sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';   // ensure termination

Using std::string directly avoids these risks. As suggested, assign the string member or read into it; for names that may contain spaces prefer std::getline so whole lines are captured safely.

The linker error about WinMain@16 means the linker did not find a standard console main entry point. That usually happens if no int main() exists or the project was set to build a GUI app (which expects WinMain). Add a main (or switch the project to a console subsystem / compile with -mconsole in MinGW). A minimal example to exercise team::create_team():

int main()
{
    team t;
    t.create_team();
    return 0;
}

One more clarification on access: class members are private by default, but a member function of a class can access private members of other instances of the same class. So temp->TeamName is accessible inside team::create_team(). That said, ’s suggestion to use a public setter/getter is good design for encapsulation. Also avoid unnecessary new allocations (use stack objects or smart pointers) and avoid strcpy because it can overflow buffers; prefer std::string and safe copy functions instead.

Recommended Answers

All 4 Replies

strcpy() works with c-strings and those are arrays of characters that have a '\0' terminating character. You are using a C++ string and they have the '=' operator overloaded so you just have to write string1 = string2; and that will copy the contents of string2 into string1.

I would not use strcpy() and I wouldn't make a new string variable 'x'. It looks like you could just write cin >> temp->TeamName; and that would work the way you want it.

Okay i'll implement that,
but now I'm getting this error

[Linker error] C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to `WinMain@16'

Any idea about this?

Do you not have a main() function in your program? If what you posted is your program and not just a snippet then you need to throw in main().

By default, class members are private. You cannot assign to temp->TeamName from a string. This is where a public setter method is appropriate, as in

class team
{
    string TeamName;
    .
    .
    .
public:
    setName(const string& name);
    .
    .
    .
};
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.