Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6145339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:46:50+00:00 2026-05-23T18:46:50+00:00

i am using different classes and also objects relavent to those different classes. Now,

  • 0

i am using different classes and also objects relavent to those different classes. Now, i want to write a seperate function which use to joint two vector to another vector. where should i write these type of seperate functions. Can i use a new header file for that? actually, i made a new header file (ConnectedSegment.h) and put my function under that. but, i got this error.

43 D:myex\ConnectedSegment.h non-member function `std::vector<int>& NeighboursToGivenValue(std::map<int, std::vector<int> >&, int, int)' cannot have `const' method qualifier 

D:\myex\Detects\Makefile.win [Build Error]  [Detects.o] Error 1 

Here is my code of the function:

vector<int> & NeighboursToGivenValue(map<int, vector<int> > &adjacency, 
                                              int s1, int s2) const
{
  vector<int>::const_iterator any;
  vector<int>::iterator is_any;
  vector<int> *neighbours2both = new vector<int>;
  int i;

  neighbours2both->reserve(adjacency[s1].size() + adjacency[s2].size());
  neighbours2both->insert(neighbours2both->end(), adjacency[s1].begin(),adjacency[s1].end ());

  vector<int>& neighbours2s2=adjacency[s2];        
  for (any=neighbours2s2.begin(); any!=neighbours2s2.end(); any++){            
       is_any = find (neighbours2both->begin(), neighbours2both->end(), *any);           
       if(is_any == neighbours2both->end()){
          if(s1 != *any) neighbours2both->push_back(*any);
       }
  }
  for (i=0; i<neighbours2both->size(); i++){
       neighbours2both->erase(neighbours2both->begin() + i);
  }
  return(*neighbours2both);
}

In here, I got the values for the adjacency() by using another class called MyPoint.
So I used myxyz.adjacency() to accomadate the values for this adjacency. Now I dont want to call the same class MyPoint for calling function NeighboursToGivenValue. So,
could you tell me where I should write this function. Alternatively, if I write this function inside the MyPoint class, how can I call this function without an object of that class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:46:50+00:00Added an answer on May 23, 2026 at 6:46 pm

    These free functions can be in the header (will need to be declared inline as Pedro highlights in his comment) – or they can be declared in the header and defined in an implementation file – for example,

    #header
    
    vector<int>& join_int_vector(vector<int> const& lhs, vector<int> const& rhs);
    
    # cpp
    
    vector<int>& join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
    {
     // do stuff
    }
    

    The problem you face is that you can only have const member functions – i.e. free functions (and static functions) cannot be const – it doesn’t mean anything.

    btw. don’t construct a vector dynamically and return a reference to it, i.e:

    vector<int> *neighbours2both = new vector<int>;
    :
    return (*neighbours2both);
    

    The callee has no awareness that this object needs to be manually cleaned up – in this case, return by value.

    vector<int> join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
    {
      vector<int> neighbours2both;
      :
      return neighbours2both;
    }
    

    EDIT: for functions defined in the header itself, something like this:

    inline vector<int> join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
    {
      vector<int> neighbours2both;
      :
      return neighbours2both;
    }
    

    note the use of the keyword inline – this prevents multiple definition errors if this header is included in multiple translation units.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the IOC concept, which we can mix-and-match different classes using wiring. Every
How to connect pyqtSignal between two different objects (classes) PROPERLY? I mean best practice.
I need to store different objects in IsolatedStorage and i'm using IsolatedStorageSettings class to
Anyone knows the difference between System.Diagnostic.Trace and System.Diagnostic.TraceSource classes? I've been using Trace for
My MySQL database contains several tables using different storage engines (specifically myisam and innodb).
I work on a variety of projects using different languages and platforms. Parts of
Some of my script are using different encoding, and when I try to combine
I have a thread that downloads some images from internet using different proxies. Sometimes
I have two tables, and they are using different collations. It is not allowed
I'm using two different libraries in my project, and both of them supply a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.