分拣程序

发布于 2025-01-07 23:41:27 字数 3871 浏览 3 评论 0原文

我正经历着最困难的时期。我什至不再理解错误消息,因为错误消息太多了。我认为我的 alphaGreater() 类函数参数有问题,但谁知道呢。任何人都可以使用 mySort() 中的冒泡排序来按字母顺序排序吗?

#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry arr[], int size)
{
    bool inOrder = false;
    string temp;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            if(arr[j+1].alphaGreater(arr[j]))
            {
                inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
};

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
            mySort(entry, count);
        }   

        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}

电话条目标题

电话编号标题

对文本进行排序 (http://pastebin.com/HE8Rsmbg)

我的错误...

>> g++ sort.cpp -o PhoneSort.exe
object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned
int)(((unsigned int)j) * 20u)))'
object.cpp:35:29: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _C
harT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, st
d::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const std::basic_s
tring<char>& {aka const std::basic_string<char>&}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_tra
its<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Al
loc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const char*'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<cha
r>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> =
std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'char'
object.cpp:37:30: error: no match for 'operator=' in '*(arr + ((((unsigned int)j
) + 1u) * 20u)) = temp'
object.cpp:37:30: note: candidate is:
phoneEntry.h:12:7: note: PhoneEntry& PhoneEntry::operator=(const PhoneEntry&)
phoneEntry.h:12:7: note:   no known conversion for argument 1 from 'std::string
{aka std::basic_string<char>}' to 'const PhoneEntry&'

I'm having the hardest time with this. I don't even understand the error messages anymore since there's so many of them. I think there's a problem with my alphaGreater() class function parameters but who knows. Can anybody get this to sort alphabetically using the bubble sort inside mySort()?

#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry arr[], int size)
{
    bool inOrder = false;
    string temp;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            if(arr[j+1].alphaGreater(arr[j]))
            {
                inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
};

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
            mySort(entry, count);
        }   

        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}

Phone Entry Header

Phone Number Header

Sorting Text (http://pastebin.com/HE8Rsmbg)

My Errors...

>> g++ sort.cpp -o PhoneSort.exe
object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned
int)(((unsigned int)j) * 20u)))'
object.cpp:35:29: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _C
harT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, st
d::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const std::basic_s
tring<char>& {aka const std::basic_string<char>&}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_tra
its<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Al
loc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const char*'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<cha
r>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> =
std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'char'
object.cpp:37:30: error: no match for 'operator=' in '*(arr + ((((unsigned int)j
) + 1u) * 20u)) = temp'
object.cpp:37:30: note: candidate is:
phoneEntry.h:12:7: note: PhoneEntry& PhoneEntry::operator=(const PhoneEntry&)
phoneEntry.h:12:7: note:   no known conversion for argument 1 from 'std::string
{aka std::basic_string<char>}' to 'const PhoneEntry&'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2025-01-14 23:41:27

您的 arr 参数包含一个 PhoneEntry 值数组。当您写入 temp = arr[j] 时,您尝试从该数组中读取一个条目并将其存储在 string 中。您无法将 PhoneEntry 分配给 string。更改 temp 为可以保存 PhoneEntry 值的类型,即 PhoneEntry

PhoneEntry temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

更好的是,使用 swap 函数来自 标头。删除 temp 变量并将上面的三行替换为以下一行:

std::swap(arr[j], arr[j + 1]);

Your arr parameter holds an array of PhoneEntry values. You attempt to read an entry from that array and store it in a string when you write temp = arr[j]. You can't assign a PhoneEntry to a string. Change temp to have a type that can hold PhoneEntry values, namely PhoneEntry:

PhoneEntry temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

Better yet, use the swap function from the <algorithm> header. Remove the temp variable and replace the three lines above with this one:

std::swap(arr[j], arr[j + 1]);
長街聽風 2025-01-14 23:41:27

尝试

arr[j+1].alphaGreater(arr[j])

而不是

arr.alphaGreater(arr[j])

但我猜真的。

Try

arr[j+1].alphaGreater(arr[j])

instead of

arr.alphaGreater(arr[j])

But I am guessing really.

余罪 2025-01-14 23:41:27

object.cpp:在函数“void mySort(PhoneEntry*, int)”中:
object.cpp:35:29: 错误: 'temp = *(arr + 中的 'operator=' 不匹配
((无符号整数)(((无符号整数)j) * 20u)))'

因此,您将 PhoneEntry 分配给 temptemp 是一个 std::string 而 PhoneEntry 不是 - 因此会出现错误。

请注意,temp 仅在最内层作用域中使用,因此您应该在那里声明它,并在同一行上初始化它。 (一般来说,你不应该在没有初始化的情况下声明局部变量。)

我还建议你看看 std::swap 函数。

object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr +
((unsigned int)(((unsigned int)j) * 20u)))'

So, you're assigning a PhoneEntry to temp. temp is a std::string and PhoneEntry is not - hence the error.

Note that temp is only used in the innermost scope, so you should declare it there, and initialise it on the same line. (In general, you should not declare a local variable without initialising it.)

I would also suggest that you might have a look at the std::swap function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文