C ++ Control到达非void的结尾

时间:2013-11-12 20:05:14

标签: c++ coding-style

我正在尝试编译这个,但它给了我那个恼人的错误。

Image imageLabeling(const Image &img, ImageLabels &imgL)
{

    long numLines=img.size();
    long numCols=img[0].size();

    int u=0;
    int v=0;
    int label=1;

    imgL.resize(numLines);
    for (unsigned int i=0; i<numLines; i++)
        imgL[i].resize(numCols);

    for (unsigned int i=0; i <numLines; i++)
        for (unsigned int j=0; j <numCols; j++)
            imgL[i][j]=0;


    for (int i=0; i<numLines; i++)
        for (int j=0; j<numCols; j++)
        {
            if(img[i][j]=='1'&&imgL[i][j]==0)
            {
                floodFill (u,v,label,img,imgL);
                label++;
            }
        }}

有什么想法吗?

P.s这是我的漏洞代码

#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream> //NOTE: needed to use files
#include <string>
using namespace std;

// Create a new type and call it 'Image'
typedef vector<vector<char>> Image;
typedef vector<vector<int>> ImageLabels;
//============================================================
// Reads an image from input stream 'f'
// 'f' can be a previously open text file or 'cin'
// and stores it into 'img'
//------------------------------------------------------------



void readImage(istream &f, Image &img)
{
    unsigned int numLines, numCols;

    cout << "Number of lines ?"<<endl;
    f>>numLines;
    f>>numCols;
    cout << "Number of columns ?"<<numCols<<endl;


    img.resize(numLines);
    for (unsigned int i=0; i<numLines; i++)
        img[i].resize(numCols);

    for (unsigned int i=0; i <numLines; i++)
        for (unsigned int j=0; j <numCols; j++)
            f >> img[i][j];
}

void readCompFile (istream &f, Image &img) //
{
    int row=0;
    int cols=0;
    char value ='0';
    f>>row;
    f>>cols;
    f>>value;
    int numPixels=0;
    vector<char> tmp;
    while(f>>numPixels)
    {
        for(int i=0;i<numPixels;i++)
        {
            tmp.push_back(value); // insere continuamente no vector tmp os valores do formato comprimido
        }
        if (value=='0')
            value='1';
        else
            value='0';
    }

    for(int i = 0;i<row;i++)
    {
        vector<char> accumulator;
        for(int j=0;j<cols;j++)
        {
            accumulator.push_back(tmp[i*row+j]); // retira os valores bin·rios da imagem, criada no vector tmp, para o vector accumulator, em linha e coluna.
        }
        img.push_back(accumulator); // retira os valores do vector acumulator, para a imagem final apresentada na consola.
    }
}

void showImage(ostream &f, const Image &img)
{
    for (unsigned int i=0; i <img.size(); i++)
    {
        for (unsigned int j=0; j <img[i].size(); j++)
            f << setw(3) << img[i][j];
        f << endl;
    }
}

void floodFill (int u, int v, int label,Image img, ImageLabels &imgL)

{
    struct Point { int x; int y;} p;


    u = p.x;
    v = p.y;

    vector <Point> stack;
    stack.push_back(p);


    while (!stack.empty())
    {
        stack.back ();
        stack.pop_back();

        u = p.x;
        v = p.y;
        //u=u+1;

        Point one = {(u+1), v};
        Point two = {u,(v+1)};
        Point three = {u,(v-1)};
        Point four = {(u-1),v};

        if ((u>=0) && (u<img.size()) && (v>=0) && (v<img[0].size()) && img[u][v]==1)
        {
            stack.push_back(one);
            stack.push_back(two);
            stack.push_back(three);
            stack.push_back(four);
        }
    }
}

void readImageLabeling(istream &f, Image &img)
{
    unsigned int numLines, numCols;

    f>>numLines;
    f>>numCols;


    img.resize(numLines);
    for (unsigned int i=0; i<numLines; i++)
        img[i].resize(numCols);

    for (unsigned int i=0; i <numLines; i++)
        for (unsigned int j=0; j <numCols; j++)
            f >> img[i][j];
}


//============================================================
// Writes an image 'img' to output stream 'f'
// 'f' can be a previously open text file or 'cout'
//------------------------------------------------------------


Image imageLabeling(const Image &img, ImageLabels &imgL)
{

    long numLines=img.size();
    long numCols=img[0].size();

    int u=0;
    int v=0;
    int label=1;

    imgL.resize(numLines);
    for (unsigned int i=0; i<numLines; i++)
        imgL[i].resize(numCols);

    for (unsigned int i=0; i <numLines; i++)
        for (unsigned int j=0; j <numCols; j++)
            imgL[i][j]=0;


    for (int i=0; i<numLines; i++)
        for (int j=0; j<numCols; j++)
        {
            if(img[i][j]=='1'&&imgL[i][j]==0)
            {
                floodFill (u,v,label,img,imgL);
                label++;
            }
        }}




Image chooseFormat(bool answer)
{
    Image imgOrg,imgComp;
    ifstream inputImg,inputImgComp;

    inputImg.open("img1.txt");
    ofstream outputImg("img2.txt");

    inputImgComp.open("img_comp1.txt");
    ofstream outputImgComp("img_comp2.txt");
    if (answer == true)
    {readImage(inputImg,imgOrg);
        showImage(cout,imgOrg);
        showImage(outputImg,imgOrg);
        return imgOrg;
    }
    else
    {
        readCompFile (inputImgComp, imgComp);
        showImage(cout,imgComp);
        showImage(outputImgComp,imgComp);
        return imgComp;
    }

}

int main()
{
    Image imgResult;
    ImageLabels imgLabel;

    bool answerFormat = false;
    string answer;
    cout<<"Read normal format?"<<endl;
    cin >> answer;
    if(answer=="yes")
        answerFormat=true;
    else
        answerFormat=false;

    imgResult=chooseFormat(answerFormat); //usa a funÁ„o answerFormat para escolher o tipo de ficheiro que se quer interpretar - imgResult
    imageLabeling(imgResult,imgLabel); // faz o labeling da imagem escolhida (normal/comprimida - imgResult)
    cout<<endl;
    showImage(cout,imgResult); // Mostra a imgResult j· etiquetada



    return 0;
};

2 个答案:

答案 0 :(得分:2)

在这一行:

Image imageLabeling(const Image &img, ImageLabels &imgL)
^^^^^

你告诉编译器你的函数'imageLabeling'应该返回'Image'类型的东西。然后你继续实际上没有在这个函数中返回任何东西。

您应该将该行更改为:

void imageLabeling(const Image &img, ImageLabels &imgL)
^^^^

要么实际返回你所承诺的'Image'。

答案 1 :(得分:0)

您的功能不会返回图像。将返回类型声明为void。