How can insert value in structure vector?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How can insert value in structure vector?

  1. #1
    Join Date
    Feb 2013
    Posts
    21

    How can insert value in structure vector?

    Here I have given my sample code, but it gave error. How can insert value in structure vector?

    Code:
    struct Hough_Transform_3D_Accumulator
    {
    	long int i;
    	long int j;
    	long int k;
    	long int count;
    };
    
    int main()
    {
    ........................
    
    for(i=0;i<total_r_count;i++)//for loop r count start
    {
    for(j=0;j<total_theta_count;j++)//for loop theta count start
    {
    for(k=0;k<total_shi_count;k++)//for loop shi count start
    {
    if(accumulator_3D[i][j][k]>0)
    {
    vec.push_back(i,j,k,accumulator_3D[i][j][k]);/// error message
    }
    }
    }
    }
    Error Message:error C2661: 'std::vector<_Ty>:ush_back' : no overloaded function takes 4 arguments

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: How can insert value in structure vector?

    Quote Originally Posted by nihad View Post
    Here I have given my sample code, but it gave error. How can insert value in structure vector?
    Give your struct a constructor.

    Second, give your struct more meaningful member names. Giving names such as "i", "j", and "k" not only doesn't convey any meaning, imagine if you had many source files, and you want to search where you used "j".
    Code:
    struct Hough_Transform_3D_Accumulator
    {
    	long int loop1;
    	long int loop2;
    	long int loop3;
    	long int count;
            Hough_Transform_3D_Accumulator(long val1 = 0, long val2 = 0, long val3 = 0, long val4 = 0) :
                    loop1(val1), loop2(val2), loop3(val3), count(val4) {}
    };
    
    int main()
    {
        //
        vec.push_back(Hough_Transform_3D_Accumulator(i,j,k,accumulator_3D[i][j][k]));
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2013
    Posts
    21

    Re: How can insert value in structure vector?

    I solved the problem. Here is the solution:

    Code:
    Hough_Transform_3D_Accumulator temp;
    
    temp.i = i;
    
    temp.j = j;
    
    temp.k = k;
    
    temp.count = accumulator_3D[i][j][k];
    
    vec.push_back(temp);

  4. #4
    Join Date
    Apr 1999
    Posts
    27,446

    Re: How can insert value in structure vector?

    Quote Originally Posted by nihad View Post
    I solved the problem. Here is the solution:
    That "solution" is really not a good one.

    1) The user must now know what the member names of the struct are. What if you decide to use better names? You now have to change all of your code to use the new member names.

    2) What if the user forgets to initialize one of the names?

    The correct solution is to write a constructor, as I pointed out in my initial post.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center