如何在OSX上编译此代码

时间:2015-07-25 05:05:31

标签: c++ c g++

考虑以下代码:

<div class="costSection"  runat="server">            
    برآورد قیمت ترجمه:
    <br />
    <asp:Label runat="server" Text="نوع ترجمه "></asp:Label>
    <asp:DropDownList ID="ddlTranslationType" runat="server">
        <asp:ListItem Value="0">فارسی به انگلیسی</asp:ListItem>
        <asp:ListItem Value="1">انگلیسی به فارسی</asp:ListItem>
        <asp:ListItem Value="2">فرانسه به فارسی</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:Label runat="server" Text="تعداد کلمه "></asp:Label>
    <asp:TextBox ID="txtWordsCount" runat="server"  CssClass="costSection" ></asp:TextBox><br />
    <asp:Button runat="server" ID="btnCalcCost" CssClass="costSection" OnClick="btnCalcCost_Click" Text="محاسبه هزینه"/>
    <asp:Label Text="" ID="lblCost" runat="server" CssClass="costSection"> </asp:Label>
</div>

此版本的C ++无法编译代码:

protected void btnCalcCost_Click(object sender, EventArgs e)
{

    int userChoice;
    try{int.TryParse(ddlTranslationType.SelectedValue,out userChoice);}
    catch(Exception ex){
        lblCost.ForeColor = System.Drawing.Color.Red;
        lblCost.Text=ex.Message;
        return;
    }
    int wordsCount;
    if (txtWordsCount.Text == null || !int.TryParse(txtWordsCount.Text,out wordsCount))
    {
        lblCost.ForeColor = System.Drawing.Color.Red;
        lblCost.Text = "لطفا تعداد کلمات را وارد کنید";
        return;
    }
    float cost=0;
    switch (userChoice)
    {
        case 0: //Farsi-English
            cost = (float)wordsCount / 250 * 13000;
            break;
        case 1: //English-Farsi
            cost = (float)wordsCount / 300 * 8000;
            break;
        case 2: //French-Farsi
            cost = (float)wordsCount / 250 * 20000;
            break;            

    }
    long estimatedCost = (long)cost;
    lblCost.ForeColor= System.Drawing.Color.Green;
    lblCost.Text = estimatedCost.ToString()+" تومان ";
}

编译成功使用此版本的g ++并添加#include <stdlib.h> #include <stdio.h> struct data { int nr; char const *value; } dat[] = { {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"} }; int data_cmp(void const *lhs, void const *rhs) { struct data const *const l = lhs; struct data const *const r = rhs; return (l->nr > r->nr) - (l->nr < r->nr); } int main(void) { struct data key = { .nr = 3 }; struct data const *res = bsearch(&key, dat, sizeof(dat)/sizeof(dat[0]), sizeof(dat[0]), data_cmp); if (!res) { printf("No %d not found\n", key.nr); } else { printf("No %d: %s\n", res->nr, res->value); } } 选项。

$ g++ -v
Target: x86_64-apple-darwin14.4.0
Thread model: posix

如果我需要在OSX中编译它,我该怎么办?

1 个答案:

答案 0 :(得分:2)

这部分代码:

int data_cmp(void const *lhs, void const *rhs) 
{
    struct data const *const l = lhs;
    struct data const *const r = rhs;

是无效的C ++代码。没有显式强制转换,您无法从const void *转换为其他任何内容。它是有效的C代码,但不是C ++。

使用C编译器进行编译,或修复代码使其成为有效的C ++。

int data_cmp(void const *lhs, void const *rhs) 
{
    struct data const *const l = (struct data *)lhs;
    struct data const *const r = (struct data *)rhs;

bsearch()的结果分配相似。您还可以使用reinterpret_cast<const struct data *>(lhs)。因此,这编译好了:

#include <stdlib.h>
#include <stdio.h>

struct data {
    int nr;
    char const *value;
} dat[] = {
    {1, "Foo"}, {2, "Bar"}, {3, "Hello"}, {4, "World"}
};

int data_cmp(void const *lhs, void const *rhs) 
{
    struct data const *const l = reinterpret_cast<const struct data *>(lhs);
    struct data const *const r = reinterpret_cast<const struct data *>(rhs);
    return (l->nr > r->nr) - (l->nr < r->nr);
}

int main(void) 
{
    struct data key = { .nr = 3 };
    struct data const *res = reinterpret_cast<struct data *>(bsearch(&key, dat, sizeof(dat)/sizeof(dat[0]), 
                                    sizeof(dat[0]), data_cmp));
    if (!res) {
        printf("No %d not found\n", key.nr);
    } else {
        printf("No %d: %s\n", res->nr, res->value);
    }
}

但是,如果打开足够的警告,最后会被告知.nr符号是C99指定的初始化程序,它在C ++中无效(并且您没有为{{指定初始化程序) 1}}元素)。

最好将C代码编译为C代码而不是C ++代码。

在Mac OS X 10.4.4 Yosemite上使用自制GCC 5.1.0和来自XCode 6.4的.value进行检查。