Thread: Function returning a structure - "no suitable constructor exists"

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Function returning a structure - "no suitable constructor exists"

    Hi guys,
    it is my first post here (and first problem) and im just starting my journey with C programming, so please be kind to me

    Here is my problem:
    I am trying to have my function "Krzywa" returning a structure with array, but i get this "vect" at the end of function highlited with an error "no suitable constructor exists to convert from vector[4] to Vector".
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    float siec [10][10];
    float previous [10][10];
    FILE *fp ;
    float fs;
    float u, t, x, theta; //predkosc, krok czasowy, dlugosc komorki, kat miedzy czyms a osia glowna
    float mi, Tmi, Tk; //
    float gamma, Teq, K, L; // cos, temp frontu, krzywizna, cieplo fazowe?
    float mi0, deltami, ms, dt; //mio, stale
    float siec [10][10];
    float previous [10][10];
    
    
    float dfsx, dfsy, n0x, n1x, n2x, n3x, n0y, n1y, n2y, n3y;
    float dolnaGranica, gornaGranica;
    struct Vector
    {
        int x;
        int y;
        };
    struct Vector vectors[4];
    
    
    
    
    void zerowanie ()
    {
        int i, k;
        for (i=0; i<10; ++i){
            for (k=0; k<10; ++k){
                siec[i][k]=0;
            }
        }
        for (i=0; i<10; ++i){
            for (k=0; k<10; ++k){
                previous[i][k]=0;
            }
        }
    }
    
    
    
    
    struct Vector Krzywa (int i, int k)
    {
        int n;
        struct Vector vect[4];
        for (n=0;n<4;n++){//jak znajduje faze stala w sasiedztwie, tworze krzywa K
                        switch (n){
                        case 0://1 wierzcholek
                        dfsx=-previous[i][k-1]-previous[i+1][k-1]+previous[i][k]+previous[i+1][k];
                         dfsy=-previous[i+1][k-1]-previous[i+1][k]+previous[i][k-1]+previous[i][k];
                               vect[0].x = -dfsx/(sqrt(dfsx*dfsx+dfsx*dfsx));
                               vect[0].y = -dfsy/(sqrt(dfsx*dfsx+dfsx*dfsx));
                               //printf("%d %d",vectors[0].x,vectors[0].y);
                               break;
                            case 1:    //2 wierzcholek
                               dfsx=-previous[i-1][k-1]-previous[i][k-1]+previous[i-1][k]+previous[i][k];
                               dfsy=previous[i-1][k-1]+previous[i-1][k]-previous[i][k-1]-previous[i][k];
                               if (dfsx==0 && dfsy==0) 
                               {
                               vect[1].x =0;
                               vect[1].y =0;
                               }
                               else {
                               vect[1].x = -dfsx/(sqrt(dfsx*dfsx+dfsx*dfsx));
                               vect[1].y = -dfsy/(sqrt(dfsx*dfsx+dfsx*dfsx));
                               break;
                               }
                            case 2:    //3 wierzcholek
                                dfsx=previous[i-1][k+1]+previous[i][k+1]-previous[i-1][k]-previous[i][k];
                                dfsy=previous[i-1][k]+previous[i-1][k+1]-previous[i][k+1]-previous[i][k];
                                if (dfsx==0 && dfsy==0) 
                                {
                                vect[2].x =0;
                                vect[2].y =0;
                                }
                                else {
                                vect[2].x =-dfsx/(sqrt(dfsx*dfsx+dfsx*dfsx));
                                vect[2].y =-dfsy/(sqrt(dfsx*dfsx+dfsx*dfsx));
                                break;
                                }
                            case 3:        //4 wierzcholek
                                dfsx=previous[i][k+1]+previous[i+1][k+1]-previous[i][k]-previous[i+1][k];
                                dfsy=-previous[i+1][k]-previous[i+1][k+1]+previous[i][k+1]+previous[i][k];
                                vect[3].x = -dfsx/(sqrt(dfsx*dfsx+dfsx*dfsx));
                                vect[3].y = -dfsy/(sqrt(dfsx*dfsx+dfsx*dfsx));
                                break;
                            }
                        }
        return vect;
    }
    Any idea what it can mean? I omit the rest of the code with main function as the problem lays somewhere in here.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the exact error message? It should have a line number that can help in discovering the source of the error.

    Furthermore, are you compiling as C or C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Thanks for quick reply.

    It's line number 96, with error as follows:

    Code:
    Error	9	error C2440: 'return' : cannot convert from 'Vector [4]' to 'Vector'
    Im compiling it as C.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, right. In Krzywa, vect is an array of 4 struct Vector objects. You declared Krzywa as returning one struct Vector object. Therefore, you must either return one struct Vector object, or allow an array of struct Vector objects to be passed to the function through a pointer to its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Well, i did as u said, and it indeed helped
    Thank you laserlight, cboard community may be proud to have you
    Last edited by Human; 06-13-2012 at 10:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-15-2011, 12:29 AM
  2. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  3. Do "Sleep()" exists in C Language?
    By ahsan_abbas in forum C Programming
    Replies: 3
    Last Post: 09-07-2003, 05:39 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM