CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2018
    Posts
    2

    error C2660: 'InputComponent::update': function does not take 1 arguments

    I'm trying to implement a basic Entity Component System into a game that I'm making, however upon trying to do so I came across this error, which usually I can just add the missing paramater in the function prototype.

    My InputComponent.h header file:
    Code:
    #pragma once
    
    #include "Player.h"
    #include "Keyboard.h"
    
    class InputComponent
    {
    public:
    	InputComponent();
    	~InputComponent();
    	
    	void update(Player& player);
    
    private:
    	static const int WALK_ACCELERATION = 1;
    };
    Here is relevant code from InputComponent.cpp:
    Code:
    void InputComponent::update(Player& player) {
    	switch (Keyboard::ArrowKeyDirection())
    	{
    	case ALLEGRO_KEY_UP:
    		player.velocity -= WALK_ACCELERATION;
    		break;
    
    	case ALLEGRO_KEY_DOWN:
    		player.velocity += WALK_ACCELERATION;
    		break;
    	case ALLEGRO_KEY_LEFT:
    		player.velocity -= WALK_ACCELERATION;
    		break;
    
    	case ALLEGRO_KEY_RIGHT:
    		player.velocity += WALK_ACCELERATION;
    		break;
    	}
    }
    Finally here is Player.h and relevant code from Player.cpp:
    Code:
    #pragma once
    
    #include "stdafx.h"
    #include "InputComponent.h"
    #include "PhysicsComponent.h"
    
    class Player
    {
    public:
    	Player();
    	~Player();
    	
    	int x, y;
    	int velocity;
    
    	//void update(Map& map, Graphics& graphics);
    	void update();
    
    private:
    	InputComponent input_;
    	PhysicsComponent physics_;
    
    };
    Code:
    void Player::update() {
    	input_.update(*this);
    
    	x += velocity;
    	//Resolve collision on world
    }

  2. #2
    Join Date
    Feb 2017
    Posts
    677

    Re: error C2660: 'InputComponent::update': function does not take 1 arguments

    In InputComponent.h you do #include "Player.h".

    And in Player.h you do #include "InputComponent.h".

    That is you are including .h files recursively. This usually means trouble and can cause unexpected spurious error messages.

  3. #3
    Join Date
    Dec 2018
    Posts
    2

    Re: error C2660: 'InputComponent::update': function does not take 1 arguments

    Quote Originally Posted by wolle View Post
    In InputComponent.h you do #include "Player.h".

    And in Player.h you do #include "InputComponent.h".

    That is you are including .h files recursively. This usually means trouble and can cause unexpected spurious error messages.
    Well I did try that, just removing #include "player.h" from InputComponent.h, another error came from that
    Code:
    1>------ Build started: Project: Game, Configuration: Debug Win32 ------
    1>InputComponent.cpp
    1>..\inputcomponent.h(11): error C2061: syntax error: identifier 'Player'
    1>..\inputcomponent.cpp(11): error C2065: 'Player': undeclared identifier
    1>..\inputcomponent.cpp(11): error C2065: 'player': undeclared identifier
    1>..\inputcomponent.cpp(11): error C2761: 'void InputComponent::update(void)': redeclaration of member is not allowed
    1>..\inputcomponent.cpp(11): error C2448: 'InputComponent::update': function-style initializer appears to be a function definition
    1>PhysicsComponent.cpp
    1>..\inputcomponent.h(11): error C2061: syntax error: identifier 'Player'
    1>Player.cpp
    1>..\inputcomponent.h(11): error C2061: syntax error: identifier 'Player'
    1>..\player.cpp(12): error C2660: 'InputComponent::update': function does not take 1 arguments
    1>Generating Code...
    1>Done building project "Game.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: error C2660: 'InputComponent::update': function does not take 1 arguments

    What you have is basically this

    Code:
    class InputComponent
    {
        void update(Player&);
    };
    
    class Player
    {
        InputComponent input_;
    }
    Unfortunately, this is a no-no as you have a circular reference. InputComponent depends upon Player which in turn depends upon InputComponent.

    Possibly the easiest way in this case is to have InputComponent update() take a pointer argument rather than a reference. This gives

    Code:
    class Player;
    
    class InputComponent
    {
        void update(Player*);
    };
    
    class Player
    {
        InputComponent input_;
    }
    with the appropriate changes to the .cpp files.

    This works as Player is forward referenced as a class and used as a pointer so the compiler doesn't need to know its size as the size of all pointers are the same for the same 32/64 bit build.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: error C2660: 'InputComponent::update': function does not take 1 arguments

    Quote Originally Posted by Matiasmunk View Post
    Well I did try that, just removing #include "player.h" from InputComponent.h, another error came from that
    A recursive dependency is a sign of a design problem called tight coupling. A common way to loosen it up it is to introduce a new class that breaks the dependency.
    Last edited by wolle; December 10th, 2018 at 06:02 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured