• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

구초체 초기화에 있어서 비주얼 스튜디오에서는 되는데, 리눅스에서는 에러가 나는 이유를 알고 싶습니다.

22.03.19 07:56 작성 조회수 169

0

#include <iostream> using namespace std; struct { int number; char name[10]; char phone_number[15]; int age; }typedef Employee; int main() { Employee employee[10]; employee[0] = { 1, "test1","010-5555-6666",30 }; int i; for (i = 0; i < 10; i++) { if (employee[i].age >= 20 && employee[i].age <= 30) { std::cout << employee[i].name << std::endl; } } return 0; }
 
컴파일
g++ a.cpp -std=c++11
 
에러
a.cpp: In function ‘int main()’:
a.cpp:13:14: error: no match for ‘operator=’ (operand types are ‘Employee’ and ‘<brace-enclosed initializer list>’)
employee[0] = { 1, "test1","010-2222-3333",25 };
 

답변 1

답변을 작성해보세요.

0

강민철님의 프로필

강민철

2022.03.20

안녕하세요 :)

 

음.. 코드를 다시 확인해보시겠어요?

제시해 주신 다음 코드를 제가 g++로 빌드했을 때에는 문제없이 빌드됩니다.

 

#include <iostream>
using namespace std;

struct {
        int number;
        char name[10];
        char phone_number[15];
        int age;
}typedef Employee;

int main() {

        Employee employee[10];

        employee[0] = { 1, "test1","010-5555-6666",30 };
        int i;

        for (i = 0; i < 10; i++) {
                if (employee[i].age >= 20 && employee[i].age <= 30) {
                        std::cout << employee[i].name << std::endl;
                }
        }

        return 0;
}

 

 빌드 결과는 다음과 같습니다. 

 

minchul@minchul:~/workspace/source$ g++ foo.cpp
minchul@minchul:~/workspace/source$ ./a.out
test1
minchul@minchul:~/workspace/source$ g++ foo.cpp -std=c++11
minchul@minchul:~/workspace/source$ ./a.out
test1