"Undefined symbols for Architecture x86_64 "未使用模板,可能存在语法错误

"Undefined symbols for Architecture x86_64 " no templates used, possible syntax error

本文关键字:错误 语法 存在 未使用 symbols Undefined for Architecture x86      更新时间:2023-10-16

我知道这是一个常见的问题,但请耐心等待。根据我对过去问题的理解:

  1. 我没有使用模板,所以在每次使用模板之前都会使用类似于"模板类TreeNode;"的东西,但我认为问题是类似的,与语法有关的,或者是非常基本的。如果我得到了答案,我会编辑这个问题,让更多的人从中受益

所以我所要做的就是使用这些代码在main.cpp中进行一些操作来执行一些操作,例如使用DeleteItem、Retrieve 10和打印是否找到等。

代码如下:

//unsortedtype.h    
#ifndef UNSORTEDTYPE_H_INCLUDED
#define UNSORTEDTYPE_H_INCLUDED
#include "itemtype.h"
class UnsortedType
{
    public:
        UnsortedType();
        bool IsFull() const;
        int LengthIs() const;
        void RetrieveItem(ItemType&item, bool&found);
        void InsertItem(ItemType item);
        void DeleteItem(ItemType item);
        void ResetList();
        void GetNextItem(ItemType& item);
        void PrintList();
    private:
        int length;
        ItemType info[MAX_ITEMS];
        int currentPos;
};
#endif // UNSORTEDTYPE_H_INCLUDED

//fileno2

//unsortedtyoe.cpp
#ifndef UNSORTEDTYPE_CPP_INCLUDED
#define UNSORTEDTYPE_CPP_INCLUDED
#include "unsortedtype.h"
UnsortedType::UnsortedType()
{
    length = 0;
    currentPos = -1;
}
bool UnsortedType::IsFull() const
{
    return (length == MAX_ITEMS);
}
int UnsortedType::LengthIs() const
{
    return length;
}
void UnsortedType::RetrieveItem(ItemType &item, bool &found){
    int location = 0;
    bool moreToSearch = (location < length);
    found = false;
    while (moreToSearch && !found){
        switch (item.ComparedTo(info[location]))
        {
        case LESS:
        case GREATER:
            location++;
            moreToSearch = (location < length);
            break;
        case EQUAL:
            found = true;

        }
    }
}
void UnsortedType::InsertItem(ItemType item)
{
    info[length] = item;
    length++;
}
void UnsortedType::DeleteItem(ItemType item){
    int location = 0;
    while (item.ComparedTo(info[location]) != EQUAL){
        location++;
        info[location] = info[length - 1];
        length++;
            }
}
void UnsortedType::ResetList(){
    currentPos = -1;
}
void UnsortedType::GetNextItem(ItemType &item){
    currentPos++;
    item = info[currentPos];
}
void UnsortedType::PrintList(){
    ItemType it;
    for (int i = 1; i <= length; i++){
        GetNextItem(it);
        it.Print();
        cout<<"";
    }
    ResetList();
    cout<<endl;
}

#endif // UNSORTEDTYPE_CPP_INCLUDED

//fileno3

//itemtype.h
#ifndef ITEMTYPE_H_INCLUDED
#define ITEMTYPE_H_INCLUDED
#include <iostream>
using namespace std;
const int MAX_ITEMS = 5;
enum RelationType{LESS, GREATER, EQUAL};
class ItemType{
public:
    ItemType();
    RelationType ComparedTo(ItemType) const;
    void Print() const;
    void Initialize(int number);
private:
    int value;
    } ;
#endif // ITEMTYPE_H_INCLUDED

fileno4

//itemtype.cpp
#ifndef ITEMTYPE_CPP
#define ITEMTYPE_CPP
#include "itemtype.h"
ItemType::ItemType(){
value = 0;
}
RelationType ItemType::ComparedTo(ItemType otheritem) const
{
    if (value<otheritem.value)
        return LESS;
    else if (value > otherItem.value)
        return GREATER;
    else
        return EQUAL;
}
void ItemType::Initialize(int number){
value = number;
}
void ItemType::Print() const
{
    cout << value << "";
}
#endif // ITEMTYPE_CPP

#include <iostream>
#include "itemtype.h"
#include "unsortedtype.h"
using namespace std;
int main()
{
    UnsortedType ustype1;
    cout << ustype1.LengthIs() << endl;
/*
    //insertItem
    ustype1.InsertItem(5);
    ustype1.InsertItem(7);
    ustype1.InsertItem(6);
    ustype1.InsertItem(9);
    ustype1.PrintList();
    ustype1.InsertItem(1);
    ustype1.PrintList();
    //RetreiveItem
    bool found;
    ustype1.RetrieveItem(4, found);
    cout << found << endl;
    ustype1.RetrieveItem(5, found);
    cout << found << endl;
    ustype1.RetrieveItem(9, found);
    cout << found << endl;
    ustype1.RetrieveItem(10, found);
    cout << found << endl;
    //isFULL
    cout << ustype1.IsFull() << endl;
    ustype1.DeleteItem(5);
    cout << ustype1.IsFull() << endl;
    //deleteItem
    ustype1.DeleteItem(1);
    ustype1.PrintList();
    ustype1.DeleteItem(6);
    ustype1.PrintList();

*/
    cout << "Hello world!" << endl;
    return 0;
}

我的第一个问题是,当我去掉main中的注释行,只做IsLength时,我会得到:https://i.stack.imgur.com/49JMw.png

我认为这可能是一个非常基本的错误,如果你能告诉我如何解决这个问题,我将非常感激。

PPS>可能有点懒,但当我在Main中使用注释掉的代码时,我收到了这个错误,有人能快速告诉我如何将整数作为ItemType插入到这些函数中吗?

https://i.stack.imgur.com/JOhia.png

编辑:图像1:

-------------- Build: Debug in UnsortedList (compiler: GNU GCC Compiler)---------------
g++  -o bin/Debug/UnsortedList obj/Debug/main.o obj/Debug/unsortedtype.o   -lstdc++
Undefined symbols for architecture x86_64:
  "ItemType::ItemType()", referenced from:
      UnsortedType::UnsortedType()in unsortedtype.o
      UnsortedType::UnsortedType()in unsortedtype.o
      UnsortedType::PrintList()      in unsortedtype.o
  "ItemType::ComparedTo(ItemType) const", referenced from:
      UnsortedType::DeleteItem(ItemType)       in unsortedtype.o
      UnsortedType::RetrieveItem(ItemType&, bool&)  in unsortedtype.o
  "ItemType::Print() const", referenced from:
      UnsortedType::PrintList()      in unsortedtype.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

您应该在命令行上指定itemtype.o,如下所示。

g++-o bin/Debug/UnsortedList obj/Debug/main.o obj/Debug/unsortedtype.obj/Debug/itemtype.o

编译main() 时未指定itemtype.o

g++ -o bin/Debug/UnsortedList obj/Debug/main.o obj/Debug/unsortedtype.o obj/Debug/itemtype.o