#include <iostream>
using namespace std; struct salary{
string name;
float  income;
float  allowance;
}; void main () {
salary workerInfo[5] = {
{"James", 2000.0, 200.0},
{"Benny", 3000.0, 300.0},
{"Marie", 4000.0, 300.0},
{"Johns", 3000.0, 500.0},
{"Jacks", 5000.0, 700.0}
};
}程序编译出错:Compiling...
exercise.cpp
E:\exercise.cpp(270) : error C2440: 'initializing' : cannot convert from 'char [6]' to 'struct salary'
        No constructor could take the source type, or constructor overload resolution was ambiguous
E:\exercise.cpp(270) : error C2440: 'initializing' : cannot convert from 'const double' to 'struct salary'
        No constructor could take the source type, or constructor overload resolution was ambiguous
E:\exercise.cpp(270) : error C2440: 'initializing' : cannot convert from 'const double' to 'struct salary'
        No constructor could take the source type, or constructor overload resolution was ambiguous
E:\exercise.cpp(270) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.如果将"string name"改为"char name[6]"编译通过了, 请问这是怎么回事, 难道string就不能用吗 ?
请问各位.

解决方案 »

  1.   

    同问
    #include "stdafx.h"
    #include <iostream>
    #include "string.h"
    using namespace std; struct salary{
    string name;
    float  income;
    float  allowance; salary(char str[6],float incomepara,float allowancepara){
    string name(str);
    income=incomepara;
    allowance=allowancepara;
    };
    };
    void main () {
    salary workerInfo[5] = {
    {James", 2000.0, 200.0},
    {"Benny", 3000.0, 300.0},
    {"Marie", 4000.0, 300.0},
    {Johns", 3000.0, 500.0},
    {"Jacks", 5000.0, 700.0}
    };

    }这样为什么也不可以
      

  2.   

    {"James", 2000.0, 200.0},改为{string("James"), 2000.0, 200.0}
      

  3.   

    salary workerInfo[5] = {
    {string("James"), 2000.0, 200.0},
      

  4.   

    不要#include "string.h"改成#include <string>
      

  5.   

    {_T("James"), 2000.0, 200.0},
      

  6.   

    谢谢各位,不过我上机反复调试,好像还是不行,具体如下所示:
    #include <iostream>
    using namespace std; struct salary{
    string name;
    float  income;
    float  allowance;

    salary (string na, float inc, float allow) {
    name = na;
    income = inc;
    allowance = allow;
    }
    }; void main () {
    salary sal[2] = {
    (string("James"), 2300, 340),
    (string("Benny"), 3400, 450)
    };
    // ... ...
    }报错:E:\exercise.cpp(413) : error C2440: 'initializing' : cannot convert from 'struct salary *' to 'struct salary'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    E:\exercise.cpp(415) : error C2440: 'initializing' : cannot convert from 'struct salary *' to 'struct salary'
            No constructor could take the source type, or constructor overload resolution was ambiguous
    E:\exercise.cpp(415) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.
    exercise.exe - 3 error(s), 0 warning(s)请好心的高手们帮帮忙!
      

  7.   

    这样改一下就可以了:    salary sal[2] = {
            salary(string("James"), 2300, 340),
            salary(string("Benny"), 3400, 450)
        };