Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1342|回复: 4
打印 上一主题 下一主题

C++ 问题 ( 已解决)

[复制链接]

0

主题

0

好友

31

积分

初级会员

Rank: 1

跳转到指定楼层
1#
发表于 2009-9-11 10:43 PM |只看该作者 |倒序浏览
//This is file LinkedList.cpp

#include "LinkedList.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;

LinkedList::size_type LinkedList::count(const value_type& target) const
{
        size_type answer;
        const Node *cursor;
        answer = 0;
        cursor = list_search(head_ptr, target);
      
        while(cursor!=NULL)
        {
                ++answer;
                cursor = cursor->next;
                cursor = list_search(cursor,target);  //<--------error
        }
        return answer;

}


//This is file LinkedList.h

#ifndef LinkedList_h
#define LinkedList_h
#include "Node.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

class LinkedList
{
    public:
        typedef Node::value_type value_type;
        typedef std::size_t size_type;
      
        LinkedList();
        LinkedList(const LinkedList& source);
        ~LinkedList();
        Node* list_search(Node* head_ptr, const Node::value_type& target);
        void head_insert(const value_type& entry);
        void tail_insert(const value_type& entry);
        void operator += (const LinkedList& addend);
        size_type size() const
        {
            return many_nodes;
        }
        size_type count(const value_type& target) const;
        void list_copy(const Node* source_ptr, Node*& head_ptr, Node*& tail_ptr);
        void list_clear(Node*& head_ptr);
        Node* list_search(Node* cursor, const Node::value_type& target) const;
        const Node* list_search(const Node* head_ptr, const Node::value_type& target);
   
    private:
        Node* head_ptr;
        size_type many_nodes;
};
#endif /*LinkedList_h*/


//This is file Node.h
#ifndef Node_h
#define Node_h
#include <cstdlib>
#include <string>

class Node
{
    public:
        //Tupedef
        typedef std::string value_type;
        typedef std::size_t size_type;
      
        //Constructor
        Node();
        Node(const value_type& init_data = value_type(), Node* init_link = NULL)
        {
            data_field = init_data;
            link_field = init_link;
        }
        Node* next;
      
    private:
        value_type data_field;
        Node *link_field;
};
#endif /* Node_h */


出现了
"LinkedList.cpp:In member function 'size_t LinkedList::count(const std::string&) const':
Linkedlist.cpp:45 : error: passing 'const Linkedlist' as 'this' argument of 'const Node* LinkedList::list_search(const Node*, const std::string&)' discard qualifiers

我试过很改了很多次,还是不知道怎么改。。。
请帮帮忙。。。。。

[ 本帖最后由 alphading 于 2009-9-12 08:55 AM 编辑 ]




收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2009-9-12 12:35 AM |只看该作者
原帖由 alphading 于 2009-9-11 10:43 PM 发表
//This is file LinkedList.cpp

#include "LinkedList.h"
#include
#include
#include
#include
using namespace std;

LinkedList::size_type LinkedList::count(const value_type& target) const
{
        size_type answer;
        const Node *cursor;
        answer = 0;
        cursor = list_search(head_ptr, target);
      
        while(cursor!=NULL)
        {
                ++answer;
                cursor = cursor->next;
                cursor = list_search(cursor,target);  //<--------error
        }
        return answer;

} ...


才剛開始看前面幾行就發現你所貼出的 coding 不完整, 無法幫你檢查




p/s: 自己注意自己定義的參數類型
Node* list_search(Node* cursor, const Node::value_type& target) const;

[ 本帖最后由 Super-Tomato 于 2009-9-12 12:42 AM 编辑 ]


回复

使用道具 举报

0

主题

0

好友

31

积分

初级会员

Rank: 1

3#
发表于 2009-9-12 01:00 AM |只看该作者

回复 #2 Super-Tomato 的帖子

对不起,能不能请你翻译參數類型 的英文是什么???

因为我在australia 读书,学的都是英语,华语的我不明白. 抱歉..........

/*This is file LinkedList.cpp

#include "LinkedList.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;

LinkedList:inkedList()
{
        head_ptr = NULL;
        many_nodes = 0;
}

LinkedList:inkedList(const LinkedList& source)
{
        Node* tail_ptr;
        list_copy(source.head_ptr,head_ptr , tail_ptr);
        many_nodes = source.many_nodes;
}
  
LinkedList::~LinkedList()
{
        list_clear(head_ptr);
        many_nodes = 0;
}

LinkedList::size_type LinkedList::count(const value_type& target) const
{
        size_type answer;
        const Node *cursor;
        answer = 0;
        cursor = list_search(head_ptr, target);
        
        while(cursor!=NULL)
        {
                ++answer;
                cursor = cursor->next;
                cursor = list_search(cursor,target);
        }
        return answer;

}

这是还没写好的,我想解决这个问题才继续写别的。。。。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

4#
发表于 2009-9-12 01:06 AM |只看该作者
原帖由 alphading 于 2009-9-12 01:00 AM 发表
对不起,能不能请你翻译參數類型 的英文是什么???

因为我在australia 读书,学的都是英语,华语的我不明白. 抱歉..........

/*This is file LinkedList.cpp

#include "LinkedList.h"
#include  ...



參數 = parameters
類型 = type


也就是說你的 cursor 是 constant Node pointer 而你 search function 的第一個 parameter 卻只能接受 Node pointer, 兩者的型態不一所以無法執行編譯


回复

使用道具 举报

0

主题

0

好友

31

积分

初级会员

Rank: 1

5#
发表于 2009-9-12 08:54 AM |只看该作者
哦。。。。明白,搞定。。。

一点点的错就能让我搞半天。。。

谢谢。。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-26 02:32 AM , Processed in 0.123395 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部