我得到一个未解析的外部符号

时间:2014-12-24 17:04:36

标签: c++

你好派对的人我每次尝试编译时都会从我的编译器中得到这个错误这个错误发生在我的许多其他代码有任何想法吗?

错误3错误LNK2019:未解析的外部符号" void __cdecl viewaddressbook(void)"函数中引用了(?viewaddressbook @@ YAXXZ)" void __cdecl menu(void)" (?menu @@ YAXXZ)

错误2错误LNK2019:未解析的外部符号" void __cdecl removeentry(void)" (?removeentry @@ YAXXZ)在函数" void __cdecl menu(void)"中引用(?menu @@ YAXXZ)

#include "stdafx.h"
#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
void menu();
void addentry();
void removeentry();
void viewaddressbook();

map<string, string>names;



int main()
{
    menu();
    return 0;
}

void menu()
{
    int choice;
    cout << "1.View Address Book \n2. Add Entry \n3. Remove Entry" << endl;
    cin >> choice;
    switch (choice)
    {
    case 1:
        viewaddressbook();
        break;
    case 2:
        addentry();
        break;
    case 3:
        removeentry();
        break;
    }

}

void addentry()
{
    string email;
    string firstlast;
    cout << "Enter the persons email\n" << endl;
    cin >> email;
    cout << "Enter the person's first and last name";
    cin >> firstlast;
    names[email] = firstlast;
    cout << firstlast << " was added with the email of " << email << endl;

    cin.get();
    cin.ignore();
}

void removeentry(map<string,string> names)
{
    string shrink;
    int choice;
    string nametoremove;
    if (names.empty())
    {
        cout << "You have no one to remove\n";
        menu();
    }
    else
    {
        cout << "Who do you want to remove ?" << endl;
        cin >> nametoremove;
        map<string, string>::iterator itr = names.find(nametoremove);
        if (itr == names.end())
        {
            cout << "Sorry no one by the name of" << nametoremove;
        }
        else if (itr != names.end())
        {
            cout << "You sure you want to remove " << nametoremove << "\n\n1.Yes\n2.No" << endl;
            cin >> choice;
            switch (choice)
            {
            case 1:
                names.erase(itr);
                cout << nametoremove << " was removed";
                menu();
                break;
            case 2:
                menu();
                break;
            }
        }
    }
}

void viewaddressbook(map<string,string> names)
{
    for (map<string, string>::iterator itr = names.begin(); itr != names.end();)
    {
        cout << itr->first << "|" << itr->second;
    }
    cin.get();
    cin.ignore();
    menu();
}

2 个答案:

答案 0 :(得分:2)

您的功能

void removeentry();
void viewaddressbook();

没有定义。而是定义

void removeentry(map<string,string> names)
void viewaddressbook(map<string,string> names)

因此链接器无法解析

removeentry();
viewaddressbook();

答案 1 :(得分:0)

您将文件顶部的函数声明为:

void menu();
void addentry();
void removeentry();
void viewaddressbook();

但您使用的定义不匹配:

void removeentry(map<string,string> names)
{
   // ...
}
void viewaddressbook(map<string,string> names)
{
   // ...
}

更改顶部的声明以匹配函数定义以修复错误消息:

void menu();
void addentry();
void removeentry(map<string,string> names);
void viewaddressbook(map<string,string> names);