CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

erro: no match for operator[]

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 16, 2017, 17:59
Default erro: no match for operator[]
  #1
Member
 
Vedamt Chittlangia
Join Date: Feb 2016
Posts: 64
Rep Power: 9
vcvedant is an unknown quantity at this point
Hello,
I am modifying a turbulence model and for a part of the code I get an error:
Quote:
error: no match for 'operator[]' in 'FST[cellI]'
for the function
Code:
tmp<volScalarField> myModel::rCm
(
     const volScalarField& tke
)
{
     tmp<volScalarField> rC = tke/tke;
     tmp<volScalarField> FST = tke;
     const fvPatchScalarField* topTKE(tke.boundaryField()[2]);
     forAll(tke, cellI)
     {
          FST[cellI] = topTKE[3];
     }
     return rC;
}
I get an additional
Quote:
error: 'class Foam::tmp<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> >' has no member named 'size'
when I tried
Code:
     forAll(FST,cellI)
     {
         FST[cellI] = topTKE[3];
     }

What should I change for FST such that I can access individual value of FST for each cell in the mesh and assign it some value?

Thanks,
vcvedant is offline   Reply With Quote

Old   August 17, 2017, 12:42
Default
  #2
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Code:
FST()[cellI] = topTKE[3];
Zeppo is offline   Reply With Quote

Old   August 17, 2017, 13:49
Default
  #3
Member
 
Vu
Join Date: Nov 2016
Posts: 42
Rep Power: 10
DoQuocVu is on a distinguished road
Hi Vedamt,

As far as i know, the forAll(FST, cellI) means: for(int i=0; i< A; i++), with A is a integer which is the size of FST.

However, you defined FST as a volScalarField which is a 2D or 3D matrix according to your mesh, and size of a matrix is mxn but not a scalar so you cannot use it for the "for" loop.

The only solution I can come up with is using another scalarField:
Code:
scalarField& FSTin_ = FST.internalField();
forAll(FSTin_, cellI)
     FSTin_[cellI] = topTKE[3];
Good luck,
Vu
DoQuocVu is offline   Reply With Quote

Old   August 17, 2017, 16:13
Default
  #4
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Code:
scalarField& FSTin_ = FST.internalField();
->
Code:
scalarField& FSTin_ = FST().internalField();
Zeppo is offline   Reply With Quote

Old   August 25, 2017, 21:41
Default
  #5
Member
 
Sugajen
Join Date: Jan 2012
Location: Tempe, USA
Posts: 52
Rep Power: 14
Sugajen is on a distinguished road
Hi all,

I did a similar expression as below but got an error.
error:
Code:
error: invalid initialization of reference of type ‘Foam::scalarField& {aka Foam::Field<double>&}’ from expression of type ‘const Internal {aka const Foam::DimensionedField<double, Foam::volMesh>}’
     scalarField& varNuIn_ = varNu_().internalField();
code:
Code:
    Foam::tmp<Foam::volScalarField> varNu_;
   
    scalarField& varNuIn_ = varNu_().internalField();
Any ideas?
Sugajen is offline   Reply With Quote

Old   August 27, 2017, 12:23
Default
  #6
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Quote:
Originally Posted by Sugajen View Post
Code:
scalarField& varNuIn_ = varNu_().internalField();
If you are not going to modify the field later on:
Code:
const scalarField& varNuIn_ = varNu_();
otherwise:
Code:
scalarField& varNuIn_ = varNu_.ref();
Sugajen, vcvedant and sibo like this.
Zeppo is offline   Reply With Quote

Old   October 10, 2017, 12:57
Default
  #7
Member
 
sibo
Join Date: Oct 2016
Location: Chicago
Posts: 55
Rep Power: 10
sibo is on a distinguished road
Hi Sergel,

I have a similar error message when compiling a customized solver in OF_4.1.

It is a dynamic solver and in order to assign my calculated displacement value "dispVals" to cellDisplacement boundary, I use this line:

Quote:
PointDisplacement.boundaryField()[patchID] == dispVals;
and the error is:

Quote:
error: no match for ‘operator==’ (operand types are ‘const Foam:ointPatchField<Foam::Vector<double> >’ and ‘Foam::vectorField {aka Foam::Field<Foam::Vector<double> >}’)
PointDisplacement.boundaryField()[patchID] == dispVals;
^
here I want to use this "==" to force assignment.
Do you know how to solve this problem? Any suggestion is welcome!
Thanks a lot!
sibo is offline   Reply With Quote

Old   October 14, 2017, 07:58
Default
  #8
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Code:
PointDisplacement.boundaryField()[patchID] == dispVals;
->
Code:
PointDisplacement.boundaryFieldRef()[patchID] == dispVals;
sibo likes this.
Zeppo is offline   Reply With Quote

Old   October 14, 2017, 11:20
Default
  #9
Member
 
sibo
Join Date: Oct 2016
Location: Chicago
Posts: 55
Rep Power: 10
sibo is on a distinguished road
Thanks a lot! I solved this problem a few days ago, so I think in the new version they name the const and non-const function differently.
sibo is offline   Reply With Quote

Old   January 19, 2018, 08:00
Default
  #10
New Member
 
Nick
Join Date: Dec 2017
Location: UK
Posts: 7
Rep Power: 9
NICKTAVOUKT is on a distinguished road
Hi guys I also have a similar problem... more specifically:

When I try to compile the following:

Quote:
inline tmp<volVectorField> solidParticleCloud::momentumSource() const
{
tmp<volVectorField> tsource
(
new volVectorField
(
IOobject
(
"smom",
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedVector
(
"zero",
dimensionSet(1, -2, -2, 0, 0),
vector::zero
)
)
);
tsource().internalField() = smom_/mesh_.cellVolumes();
return tsource;
}
and I get the following error:

Quote:
solidParticleCloudI.H:91:57: error: no match for 'operator=' (operand types are 'const Internal {aka const Foam:imensionedField<Foam::Vector<double>, Foam::volMesh>}' and 'Foam::tmp<Foam::Field<Foam::Vector<double> > >')
tsource().internalField() = smom_/mesh_.cellVolumes();
Interestingly enough when I compiled the same code in OF3 I did not have any problems... The problem occurs only when compiled n OF5
I would really appreciate any help!

Thanks!
NICKTAVOUKT is offline   Reply With Quote

Old   January 19, 2018, 09:43
Default
  #11
Member
 
sibo
Join Date: Oct 2016
Location: Chicago
Posts: 55
Rep Power: 10
sibo is on a distinguished road
HI Nick,

Try
Quote:
tsource().internalFieldRef() = smom_/mesh_.cellVolumes();
I believe in the version 4.0 and after that, they seperate the const and non-const function with different names. InternalField() provides the const reference to the internal field, internalFieldRef() provides a non-const reference.
sibo is offline   Reply With Quote

Old   January 19, 2018, 10:47
Default
  #12
New Member
 
Nick
Join Date: Dec 2017
Location: UK
Posts: 7
Rep Power: 9
NICKTAVOUKT is on a distinguished road
Thanks Sibo,

I'll give it a try and see what comes out of it!
NICKTAVOUKT is offline   Reply With Quote

Old   February 6, 2018, 10:55
Default
  #13
New Member
 
Nick
Join Date: Dec 2017
Location: UK
Posts: 7
Rep Power: 9
NICKTAVOUKT is on a distinguished road
Hi all,

I found this link which describes how to update older code versions to the newest... which also gives an answer to most of the questions posed in this thread. I hope you find it helpful!

https://www.openfoam.com/documentati...rade-guide.php

Regards,
Nick
NICKTAVOUKT is offline   Reply With Quote

Old   April 25, 2018, 09:28
Default
  #14
New Member
 
Join Date: Jul 2016
Posts: 1
Rep Power: 0
JulianS is on a distinguished road
Hey everyone,

This thread already helped me a lot. Thanks for this guys!
Now I would like to do something quite similar, just the other way round as the issue solved before here:
Quote:
Originally Posted by Zeppo View Post
Code:
PointDisplacement.boundaryField()[patchID] == dispVals;
->
Code:
PointDisplacement.boundaryFieldRef()[patchID] == dispVals;
When I try to write the PointDisplacement entries to a vectorField, like this:
Code:
dispVals=PointDisplacement.boundaryFieldRef()[patchID];
I get the error message:
Code:
error: no match for ‘operator=’ (operand types are ‘Foam::vectorField {aka Foam::Field<Foam::Vector<double> >}’ and ‘Foam::pointPatchField<Foam::Vector<double> >’)
Does anyone know what I'm doing wrong or how to solve this?

Thanks!
JulianS is offline   Reply With Quote

Old   March 30, 2020, 08:14
Default Problem in printing the values
  #15
New Member
 
sujata
Join Date: Dec 2019
Posts: 10
Rep Power: 7
sujata is on a distinguished road
hello everyone.
I am using multiphaseEulerfoam solver. I am new to OpenFOAM and using version 4.1 . I am trying to print u velocity, Reynolds No in the phasepair.C file
Foam::tmp<Foam::volScalarField> Foam:hasePair::magUr() const
{
return mag(phase1().U() - phase2().U());
Info<< "magUr =" <<magUr()[0] << endl;
}


Foam::tmp<Foam::volVectorField> Foam:hasePair::Ur() const
{
return dispersed().U() - continuous().U();
}


Foam::tmp<Foam::volScalarField> Foam:hasePair::Re() const
{
return magUr()*dispersed().d()/continuous().nu();
Info<< "Re =" <<Re()[0] << endl;
}



The error I am getting is as follows

phasePair/phasePair/phasePair.C(108): error: no operator "[]" matches these operands
operand types are: Foam::tmp<Foam::GeometricField<Foam::scalar, Foam::fvPatchField, Foam::volMesh>> [ int ]
Info<< "magUr =" <<magUr()[0] << endl;
^

phasePair/phasePair/phasePair.C(121): error: no operator "[]" matches these operands
operand types are: Foam::tmp<Foam::GeometricField<Foam::scalar, Foam::fvPatchField, Foam::volMesh>> [ int ]
Info<< "Re =" <<Re()[0] << endl;
^

I want to print it for 0th cell. Can anyone help me with this.
sujata is offline   Reply With Quote

Old   March 30, 2020, 09:09
Default
  #16
Member
 
Vu
Join Date: Nov 2016
Posts: 42
Rep Power: 10
DoQuocVu is on a distinguished road
Hi Sujata,

the function mag() return a scalar, thus magUr() is a scalar and it doesn't have [] operator
sujata likes this.
DoQuocVu is offline   Reply With Quote

Old   March 30, 2020, 10:19
Default
  #17
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Quote:
Originally Posted by sujata View Post
hello everyone.
I am using multiphaseEulerfoam solver. I am new to OpenFOAM and using version 4.1 . I am trying to print u velocity, Reynolds No in the phasepair.C file
Foam::tmp<Foam::volScalarField> Foam:hasePair::magUr() const
{
return mag(phase1().U() - phase2().U());
Info<< "magUr =" <<magUr()[0] << endl;
}


Foam::tmp<Foam::volVectorField> Foam:hasePair::Ur() const
{
return dispersed().U() - continuous().U();
}


Foam::tmp<Foam::volScalarField> Foam:hasePair::Re() const
{
return magUr()*dispersed().d()/continuous().nu();
Info<< "Re =" <<Re()[0] << endl;
}



The error I am getting is as follows

phasePair/phasePair/phasePair.C(108): error: no operator "[]" matches these operands
operand types are: Foam::tmp<Foam::GeometricField<Foam::scalar, Foam::fvPatchField, Foam::volMesh>> [ int ]
Info<< "magUr =" <<magUr()[0] << endl;
^

phasePair/phasePair/phasePair.C(121): error: no operator "[]" matches these operands
operand types are: Foam::tmp<Foam::GeometricField<Foam::scalar, Foam::fvPatchField, Foam::volMesh>> [ int ]
Info<< "Re =" <<Re()[0] << endl;
^

I want to print it for 0th cell. Can anyone help me with this.
Code:
Foam::tmp<Foam::volScalarField> Foam:hasePair::magUr() const
{
    tmp<volScalarField> ret = mag(phase1().U() - phase2().U());
    Info<< "magUr =" << ret()[0] << endl;
    return ret;
}
Function mag here returns tmp<volScalarField>. You can access its first element through [0];
sujata likes this.
Zeppo is offline   Reply With Quote

Old   March 30, 2020, 10:48
Default
  #18
Senior Member
 
Zeppo's Avatar
 
Sergei
Join Date: Dec 2009
Posts: 261
Rep Power: 22
Zeppo will become famous soon enough
Quote:
Originally Posted by JulianS View Post
When I try to write the PointDisplacement entries to a vectorField, like this:
Code:
dispVals=PointDisplacement.boundaryFieldRef()[patchID];
I get the error message:
Code:
error: no match for ‘operator=’ (operand types are ‘Foam::vectorField {aka Foam::Field<Foam::Vector<double> >}’ and ‘Foam::pointPatchField<Foam::Vector<double> >’)
Does anyone know what I'm doing wrong or how to solve this?

Thanks!
Code:
dispVals=PointDisplacement.boundaryField()[patchID].primitiveField();
This way you get a copy of internal patch field. When you modify any value of dispVals no changes will be made to PointDisplacement.boundaryField()[patchID]!
Zeppo is offline   Reply With Quote

Old   March 30, 2022, 08:21
Default
  #19
New Member
 
Feng
Join Date: Apr 2017
Posts: 9
Rep Power: 9
wind_ is on a distinguished road
Quote:
Originally Posted by NICKTAVOUKT View Post
Hi guys I also have a similar problem... more specifically:

When I try to compile the following:

and I get the following error:



Interestingly enough when I compiled the same code in OF3 I did not have any problems... The problem occurs only when compiled n OF5
I would really appreciate any help!

Thanks!
Hi,Nick
I also have this problem in OF5x, and I changed
tsource().internalField() =...
->
tsource().primitiveField() =...
but it doesn't work, how to fix it?
Thanks
Feng
wind_ is offline   Reply With Quote

Old   March 30, 2022, 08:36
Default
  #20
New Member
 
Feng
Join Date: Apr 2017
Posts: 9
Rep Power: 9
wind_ is on a distinguished road
Quote:
Originally Posted by wind_ View Post
Hi,Nick
I also have this problem in OF5x, and I changed
tsource().internalField() =...
->
tsource().primitiveField() =...
but it doesn't work, how to fix it?
Thanks
Feng
This thread is the detail.Error while compiling a solidParticle library based on interFoam in OF5x
wind_ is offline   Reply With Quote

Reply

Tags
cell value, operator, size, tmp<volscalarfield>


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANSYS Meshing] Match Control, is this weird or am I stupid? Moufle ANSYS Meshing & Geometry 2 November 18, 2015 17:00
createPatch Segmentation Fault (CORE DUMPED) sam.ho OpenFOAM Pre-Processing 2 April 21, 2014 03:01
Match Control and Symmetry Boundary Condtions in a quasi 2D calculation peterputer ANSYS Meshing & Geometry 0 May 15, 2012 09:53
gmsh2ToFoam sarajags_89 OpenFOAM 0 November 24, 2009 23:50
[Commercial meshers] Trimmed cell and embedded refinement mesh conversion issues michele OpenFOAM Meshing & Mesh Conversion 2 July 15, 2005 05:15


All times are GMT -4. The time now is 18:26.