gpt4 book ai didi

C++ 链接器错误 OSX Mavericks

转载 作者:行者123 更新时间:2023-11-28 02:51:49 24 4
gpt4 key购买 nike

我在使用 g++ 链接器时遇到问题。我的代码如下:

prog3.cpp

#include "linkedlist.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

bool readFile(const char* file, linkedlist &l);

int main(int argc, char **argv)
{
int i = 0, j = 0; //Incrementers
linkedlist dictionary;
string word;
ifstream fin;

if(argc != 3)
{
//give the user an error and exit
}

if(!readFile("dictionary_short.txt", dictionary))
{
//give the user an error and exit
}
dictionary.printToCommandline();

return 0;
}

bool readFile(const char* file, linkedlist &l)
{
string tmp;

ifstream fin;
fin.open(file, ios::in);
if(fin.is_open())
{
//Read the file
while(!fin.eof())
{
fin >> tmp;
l.insert(tmp);
}

//close the file and return the array
fin.close();

return true;
}
return false;
}

链表.h

class linkedlist
{
public:
/*!
* @brief Constructor for the linked list
*/
linkedlist();
/*!
* @brief Deconstructor for the linked list
*/
~linkedlist();

/*!
* @brief inserts a word into the linked list. Returns true if sucessful.
*/
bool insert( string str );
/*!
* @brief removes a word from the linked list. Returns true if successful
*/
bool remove( string str );
/*!
* @brief finds a word in the linked list. Returns true if sucessful
*/
bool find( string str );
/*!
* @brief Increases the frequency count of a word. Returns true if sucessful
*/
bool incrementFrequency( string str );
/*!
* @brief Returns true if a node is empty.
*/
bool isEmpty();
/*!
* @brief Returns the manimum frequency of a word from the linked list.
*/
int getMaxFrequency();
/*!
* @brief Returns the number of nodes in the list.
*/
int size();
/*!
* @brief Outputs the list into a file formatted correctly.
*/
void printToFile(ostream &out);
/*!
* @brief Outputs the list to the command line in alphabetical order
*/
void printToCommandline();

private:
/*!
* @brief Structure for a node. Keeps track of the frequency for the word.
* Stores the word. Also keeps a pointer to the next word in the list.
*/
struct node
{
int frequencyCount;
string word;
node *next;
};
node *headptr;
};

链表.cpp

#include "linkedlist.h"


linkedlist::linkedlist()
{
headptr = nullptr;
}

linkedlist::~linkedlist()
{
//Function Implementation
}

bool linkedlist::insert( string str )
{
//Function Implementation
}

bool linkedlist::remove( string str )
{
//Function Implementation
}

bool linkedlist::find( string str )
{
//Function Implementation
}

bool linkedlist::incrementFrequency( string str )
{
//Function Implementation
}

bool linkedlist::isEmpty()
{
//Function Implementation
}

int linkedlist::getMaxFrequency()
{
//Function Implementation
}

int linkedlist::size()
{
//Function Implementation
}

void linkedlist::printToFile( ostream &out )
{
//Function Implementation
}

void linkedlist::printToCommandline()
{
//Function Implementation
}

我在编译(在 Mac OS X 10.9.2 上使用 g++)时遇到的错误如下:

Undefined symbols for architecture x86_64:
"linkedlist::printToCommandline()", referenced from:
_main in prog3-717fc0.o
"linkedlist::insert(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
readFile(char const*, linkedlist&) in prog3-717fc0.o
"linkedlist::linkedlist()", referenced from:
_main in prog3-717fc0.o
"linkedlist::~linkedlist()", referenced from:
_main in prog3-717fc0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]

出于好奇,我将文件复制到 Xcode 项目中,但没有出现任何错误。我该如何解决这个链接器问题?

最佳答案

你需要将linkedlist.o传递给链接器

g++ -c prog3.cpp
g++ -c linkedlist.cpp
g++ prog3.o linkedlist.o -o prog3

或者在一个命令中:

g++ prog3.cpp linkedlist.cpp -o prog3

关于C++ 链接器错误 OSX Mavericks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22869675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com