C++ undefined reference 에러

조회수 552회

main.cpp 나 Item.cpp 를 실행해보면 둘 다 undefined reference 에러가 뜨는데 초보라 여기서 더 이상 고칠수가 없네요. 무엇이 문제인지 알려주시면 정말 감사하겠습니다.

main.cpp
---------------
#include <iostream>
#include "Item.h"

int main(){
Item item1{ "Potato Chips", 1.78 };
Item item2{ "Doritos", 1.99, 2 };

std::cout << item1.get_name() << '\n';

std::cout << "Original " << item2.get_name()  << '\n';;
std::cout << "Price:   " << item2.get_price() << '\n';
item2.set_price(2.49);
std::cout << "Updated  " << item2.get_name()  << '\n';;
std::cout << "Price:   " << item2.get_price() << '\n';
    return 0;
}


Item.h
-----------
#ifndef ITEM_H
#define ITEM_H
#include <string>

class Item {
  public:
    Item(std::string name, double price, int quantity=1);
    std::string get_name() const;
    double get_price() const;
    int get_quantity() const;

    void set_price(double new_price);
    void set_quantity(double new_quanity);
  private:
    std::string name;
    double      price;
    int         quantity;
};

#endif



Item.cpp
--------------
#include "Item.h"
#include <iostream>
using std::string;

Item::Item(std::string name, double price, int quantity){
    this->name      = name;
    this->price     = price;
    this->quantity  = quantity;}

    string Item::get_name() const{
    return name;
}

double Item::get_price() const{
    return price;
}

int Item::get_quantity() const{
    return quantity;
}

void Item::set_price(double new_price){
    price = new_price;
}

void Item::set_quantity(double new_quanity){
    price = new_quanity;
}
  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 전체 코드를 올렸는데 혹시 길이가 길다면 죄송합니다. 알 수 없는 사용자 2021.1.26 07:25
  • 에러메시지 전체를 올려주세요. undefined reference 라면, 뒤쪽에 어떤 걸 undefined 라고 하는지가 나오는데, 그걸 알면 문제의 원인을 찾는 큰 힌트가 됩니다. nowp 2021.1.26 08:26
  • 참고하여 다음부터 그렇게 하도록 하겠습니다, 감사합니다 ^^ 알 수 없는 사용자 2021.1.29 04:47

1 답변

  • 돌려 봤는데 에러도 없고 실행도 잘 됩니다.

    코드 문제가 아니고 컴퓨터 또는 통합개발환경 문제 같습니다.

    이미지

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)