//
//      Media.h   define the class cell 
//

#ifndef MEDIA_H
#define MEDIA_H

#include<vector>
#include<string>
using namespace std;

class Media
	{
	private:
	   // media name
		 string name;

	   // the density vector
	   vector<double> density;

	   // the capacity vector
	  vector<double> cp;

	 // the thermal conductivity vector
	 vector<double> thermalConductivity;

	 // the viscorsity vector
	 vector<double> viscorsity;
	public:
 

	 // default constructor initilized with name and mesh*
	 Media(){ }

	 ~Media(){}
     
	 void reSizeAll(const int& size) 
	 {

	 	 density.resize(size);
		 cp.resize(size);
		 thermalConductivity.resize(size);
		 viscorsity.resize(size);
	 }
	 
	 // set density vector with a constant value
	 void setDensity(const double & aVal) 
	 { 
		 vector<double>::iterator it;
		 for( it=density.begin(); it!=density.end(); it++ ) *it=aVal;
	 }

	 //  get the density vector
	vector<double>& getDensity() const {return density;}

      };// end of the class defination


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

 #endif

I want to return the vector by reference in the function getDensity. but the vs2008 gives me the following error:

1>g:\computation\c++cfd\flowingheat\flowingheat\media.h(58) : error C2440: 'return' : cannot convert from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'

I have not declare the vector density as const vector density, what is the reason for this problem.
also in the getDensity(), i have not change the vector,i think declar getDensity() as getDensity() const is correct.

You need to be consistent with the const-qualifier, either you have a const function that returns a const reference or you have a non-const function that return a non-const reference, but usually, you can just do both:

const vector<double>& getDensity() const {return density;}
    vector<double>& getDensity() {return density;}

The compiler will select the correct overload depending on the const-ness of the object the "getDensity" function is called with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.