Error: "No overload for method "foo" takes '4' arguments"

I have referenced a C++ project to call a function from a class within but I get the error message "No overload for method "foo" takes '4' arguments" when I compile.

if I go to the deinition for the class of the following call: myClass.foo(a, b, ref c, d)
I can see the following metadata:

namespace SPTI
{
    public class myClass

    {
        public myClass();


        public static bool foo(Target a, byte[] b, byte[] c, bool d);
    }
}

With the following for the declaration in C++:

static bool myClass::foo([Runtime::InteropServices::In]SESTool::Target a,
          [Runtime::InteropServices::In]array<Byte>^ b,
          [Runtime::InteropServices::In, Runtime::InteropServices::Out]array<Byte>^ c,
          [Runtime::InteropServices::In]bool d);

I also noticed that autocomplete will not list that function from within that particular file (it will autocomplete in other files in the C# project but not build).  Is there something that I am forgetting to include   I'm completely stumped on this one.

Thanks,

Devan



Answer this question

Error: "No overload for method "foo" takes '4' arguments"

  • Tryst

    I had thought that [Runtime::InteropServices::In, Runtime::InteropServices::Out]^ implied ref.  Please correct me if I'm wrong as I don't know that there is a "ref" keyword in C++.  The error is complaining that no function exists with 4 arguments, it does not mention a type mismatch.  :(


  • rajender reddy

    I thought using Runtime::InteropServices::In, Runtime::InteropServices::Out meant using "ref" in C#:
    myClass.myFunc(a, b, ref c, d);

    But, there is an error for that; "no overload that takes 4 parameters" seems strange.

    Are you missing a reference caret (^) on the Target argument of the myFunc declaration



  • Jonathon Stevens

    Appologies for being so brief before, I actually wasn't near the code.

    Made an example solution today with the same problem.
    The Solution comtains two projects, a C# project (InteropExample) and a C++ project (myClass). 
    Inside the C++ project is one file (aside from stdafx), myClass.cpp.  
    Inside the C# project are two non-default files, Program.cs and Target.cs.
    The C++ project has a reference to the executible object of the C# project while the C# project has a reference to the C++ project, this was done to eliminate circular dependencies but allow the C++ project to access the C# defined classes.  In InteropExample a call is made to a static method of myClass (myCPPNamespace::myClass.myFunc) that requires 4 parameters.  When I attempt to compile I get an error "No Overload for method 'myFunc' takes '4' arguments," the metadata produced from Program.cs with "Go To Definition" shows the expected function.

    Here are some of the files:
    Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    using myCPPNamespace;
    using SESTool;

    namespace IntropExample
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Target a;
                byte [] b = new byte[10];
                byte [] c = new byte[10];
                bool d;

                myClass.myFunc(a, b, c, d);

            }
        }
    }

    myClass.cpp:

    // SPTI.cpp : Defines the entry point for the DLL application.
    //

    #include "stdafx.h"

    #pragma managed

    using namespace System;

    namespace myCPPNamespace
    {
     public ref class myClass
     {

     public:
      myClass(void){};
      
      static bool myClass::myFunc([Runtime::InteropServices::In]SESTool::Target a,
              [Runtime::InteropServices::In]array<Byte>^ b,
              [Runtime::InteropServices::In, Runtime::InteropServices::Out]array<Byte>^ c,
              [Runtime::InteropServices::In]bool d)
      { return true; };
     };
    }

    Target.cs:

    using System;
    using System.Collections.Generic;
    using System.Text;


    namespace SESTool
    {
        public class Target
        {


            //---------------------------------------------------------------------
            public Target()
            {

            }


        }
    }

    myClass[from metadata]:

    using SESTool;
    using System;

    namespace myCPPNamespace
    {
        public class myClass
        {
            public myClass();

            public static bool myFunc(Target a, byte[] b, byte[] c, bool d);
        }
    }


  • dxx

    It's probably because there is no overload that takes those types of 4 arguments.

    See Short but complete programs on how to post code samples that provide enough information for someone to accurately help you.



  • ehsan707

    You've not defined byte[] c as ref. ....or should it be out

  • chaza

     Peter Ritchie wrote:
    Can you post the line that causes the "no overload for..." error.

    For the example above the line is:

    return myClass.foo(a, b, ref c, d);

    In the test code the line looks more like this:

    return SPTIWrapper.SendAspi32Command(target, cdb, ref buffer, write);

    where Target target, byte [] cdb, ref byte[] buffer, bool write

    Devan


  • su45937

    Thanks Peter, that was it.
  • isaacb

    Can you post the line that causes the "no overload for..." error.


  • Juergen Lorenz

    Peter Ritchie wrote:
    DevanL wrote:
    Thanks Peter, that was it.

    The "ref" or the caret

    The caret on 'target'


  • AlexBB

    DevanL wrote:
    Thanks Peter, that was it.
    The "ref" or the caret

  • Error: "No overload for method "foo" takes '4' arguments"