| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:C++编译错误
只看楼主 加入收藏
focus_dp
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-1-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
C++编译错误
main函数:
#include "maxsubarray.h"
#include <iostream>
using namespace std;
using namespace maxsubarray;
int main () {
    int num[6] = {1, 2 ,4, -1, 1, -1};
    Array *a = max_sub_array(num, 0, 5);
    cout<<a->left<<" ";
    cout<<a->right<<" ";
    cout<<a->sum<<endl;
    return 0;
}

头文件:
#ifndef MAX_SUB_ARRAY_H
#define MAX_SUB_ARRAY_H
namespace maxsubarray {
struct Array
{
    /* data */
    int left;
    int right;
    int sum;
};
Array* max_sub_array(int* array, int left, int right);
Array* max_crossing_sub_array(int* array, int left, int right, int mid);
}
#endif

源文件:
#include "maxsubarray.h"
using namespace maxsubarray;
Array* max_sub_array(int* array, int left, int right) {
    if (left == right) {
         Array *a = new Array;
         a->left = left;
         a->right = right;
         a->sum = array[left];
         return a;
    }
    int mid = (right - left) / 2 + left;
    Array *max_left = max_sub_array(array, left, mid);
    Array *max_right = max_sub_array(array, mid+1, right);
    Array *max_crossing = max_crossing_sub_array(array, left, right, mid);
    if (max_left->sum > max_right->sum)
        if (max_left->sum > max_crossing->sum)
            return max_left;
        else
            return max_crossing;
    else
        if (max_right->sum > max_crossing->sum)
            return max_right;
        else
            return max_crossing;
}
Array* max_crossing_sub_array(const int* array, int left, int right, int mid) {
    int max_sum_left = array[mid];
    int max_sum_right = array[mid];
    int sum = array[mid];
    int max_left = mid -1;
    int max_right = mid + 1;
    int i;
    //find the max_left
    for (i = mid-1; i >= left; i--) {
        sum += array[i];
        if (sum > max_sum_left) {
            max_sum_left = sum;
            max_left = i;
        }
    }
    // find the max_right
    sum = array[mid];
    for (i = mid+1; i <= right; i++) {
        sum += array[i];
        if (sum > max_sum_right) {
            max_sum_right = sum;
            max_right = i;
        }
    }
    Array *a = new Array;
    a->left = max_left;
    a->right = max_right;
    a->sum = max_sum_right + max_sum_left - array[mid];
    return a;
}

在linux下用G++ 编译结果:
dp@dp-ThinkPad-Edge-E430:~/Alogrithm/MaxSubArray$ g++ -o main main.cpp maxsubarray.cpp
maxsubarray.cpp: In function ‘maxsubarray::Array* max_sub_array(int*, int, int)’:
maxsubarray.cpp:14:50: error: call of overloaded ‘max_sub_array(int*&, int&, int&)’ is ambiguous
maxsubarray.cpp:14:50: note: candidates are:
maxsubarray.cpp:4:8: note: maxsubarray::Array* max_sub_array(int*, int, int)
In file included from maxsubarray.cpp:1:0:
maxsubarray.h:14:8: note: maxsubarray::Array* maxsubarray::max_sub_array(int*, int, int)
maxsubarray.cpp:15:54: error: call of overloaded ‘max_sub_array(int*&, int, int&)’ is ambiguous
maxsubarray.cpp:15:54: note: candidates are:
maxsubarray.cpp:4:8: note: maxsubarray::Array* max_sub_array(int*, int, int)
In file included from maxsubarray.cpp:1:0:
maxsubarray.h:14:8: note: maxsubarray::Array* maxsubarray::max_sub_array(int*, int, int)
dp@dp-ThinkPad-Edge-E430:~/Alogrithm/MaxSubArray$

希望高手解答
搜索更多相关主题的帖子: include return 
2014-01-09 21:41
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
你连namespace都不了解?
第一,
将 maxsubarray.cpp 中的 using namespace maxsubarray;
改为 namespace maxsubarray { ………… },和 maxsubarray.h 中一样。
否则就是声明一个 maxsubarray::max_sub_array,定义一个 ::max_sub_array。

第二,
头文件中写的是 Array* max_crossing_sub_array(int* array, ……
实现文件中写的是 Array* max_crossing_sub_array(const int* array, ……
2014-01-10 08:40
focus_dp
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-1-9
收藏
得分:0 
回复 2楼 rjsp
以前没怎么写过命名空间, 今天查了一下C++ Prime, 接口和实现是都要包含在命名空间里面的.
谢谢了。
2014-01-10 15:45
快速回复:C++编译错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022114 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved