简体   繁体   中英

does not contain a constructor

Type definitions

using System;
    
public enum Direction { Right, Left, Forward };

class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }

    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }

    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }

    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }

    class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}

Usage

class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }

        Console.WriteLine();

        horse ob = new horse();

        ob.Jump(minAge);

        ob.move();

        ob.Print();

        Console.ReadKey();
    }
}

I get these errors in Visual Studio:

'Chaharpa' does not contain a constructor that takes 0 arguments

The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

  1. In the class Chaharpa you defined two constructors, both take arguments. Creating your own constructor overrides the default constructor. Usually, when inheriting from a base class you want to initialize the inheriting class with parameters that are used to initialize the base class, look more at this thread .

  2. The horse class inside Chaharpa and Chaharpa class are not public. Changing classes horse and Chaharpa to public and accessing it as: Chaharpa.horse ob = new Chaharpa.horse(); is the right way to go.

Here are some mitigations to the code:

using System;
using System.ComponentModel;

public enum Direction
{ Right, Left, Forward };
public class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
    public Chaharpa()
    {
    }

    public Chaharpa(int a, int b, int x, int y)
    {
        age = a;
        height = b;
        cordinates_x = x;
        cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get { return age; }
    }

    public int Height
    {
        get { return height; }
    }

    public int Cordinates_x
    {
        get { return cordinates_x; }
        set { cordinates_x = value; }
    }
    public int Cordinates_y
    {
        get { return cordinates_y; }
        set { if (value > 0) cordinates_y = value; }
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
    public class horse : Chaharpa
    {
        public bool is_wild;

        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;        
        }

        public void move()
        {
            throw new NotImplementedException();
        }

        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;

        for (int i = 0; i < 3; i++)
        {

            Console.WriteLine("Horse #" + (i + 1));

            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());

            minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

        Chaharpa.horse ob = new Chaharpa.horse();

        ob.Jump(minAge);
                
        // You can call the Move defined in Chaharpa
        ob.Move(<PASS DIRECTION PARAMETER HERE>);

        ob.Print();

        Console.ReadKey();

    }
}

First, you have to study more about object creation in C#. When creating an object of a child class inside the child class constructor, it's calling the base class constructor.

using System;
                    
public class Program
{
    public static void Main()
    {
        Child child = new Child();
    }
}

public class Parent{
    
    public Parent(){
        Console.WriteLine("I am parent");
    }
    
}

public class Child : Parent {
    public Child(){
        Console.WriteLine("I am child");
    }
}

Output

I am parent
I am child

When you creating a class there is a default constructor (the constructor which takes 0 arguments). But when you create another constructor (which takes more than 0 arguments) default constructor will replaced by that. You have to manually create default constructor. So at the runtime, it's trying to call the default constructor of the base class constructor. But since it's gone this error occurs.

  • 'Chaharpa' does not contain a constructor that takes 0 arguments

Then you have to study about nested classes in C#.

You can't just call inner class by it's name. You have to reference it from the outer class.

using System;
                    
public class Program
{
    public static void Main()
    {
        Outer.Inner child = new Outer.Inner();
    }
}

public class Outer{
    
    public Outer(){
        Console.WriteLine("I am outer");
    }
    
    public class Inner : Outer {
        public Inner(){
            Console.WriteLine("I am inner");
        }
    }
}

Output

I am outer
I am inner

That's why you get the error The type or namespace name 'horse' could not be found (are you missing a using directive or an assembly reference?)

This code will work.

using System;
public enum Direction
{ Right, Left,Forward};


 class Chaharpa
{
    private int age;
    private int height;
    private int cordinates_x;
    private int cordinates_y;
     
     public Chaharpa(){}
     
    public Chaharpa(int a, int b, int x, int y)
    {
    age = a;
    height = b;
    cordinates_x = x;
    cordinates_y = y;
    }
    public Chaharpa(int c, int d)
    {
        age = c;
        height = d;
        cordinates_x = 0;
        cordinates_y = 0;
    }
    public int Age
    {
        get{ return age; }
    }

    public int Height
    {
        get{ return height;}
    }

    public int Cordinates_x
    {
        get{ return cordinates_x;}
        set{ cordinates_x = value;}
    }
    public int Cordinates_y
    {
        get{return cordinates_y;}
        set{ if (value > 0)cordinates_y = value;}
    }

    public void Move(Direction direction)
    {
        if (direction == Direction.Forward)
            Cordinates_y++;
        else if (direction == Direction.Right)
            Cordinates_x++;
        else if (direction == Direction.Left)
            Cordinates_x--;
    }
     
     public class horse : Chaharpa
    {
        public bool is_wild;
       
        public void Jump(int x)
        {
            x = Cordinates_y;
            Cordinates_y += 5;
        }
        public void Print()
        {
            Console.WriteLine("Horse Information: Age," + Age + ", Height: " + Height + ", Wild: " + is_wild + ", X: " + Cordinates_x + ", Y: " + Cordinates_y);
        }
        
    }
}


public class Program
{
    public static void Main(string[] args)
    {
        int age, x, y, minAge = 0;
        int height;
        bool wild;
         
        for (int i = 0; i < 3; i++)
        {
            
            Console.WriteLine("Horse #" + (i + 1));
    
            Console.Write("Enter Age: ");
            age = int.Parse(Console.ReadLine());
            Console.Write("Enter Height: ");
            height = int.Parse(Console.ReadLine());
            Console.Write("Enter X: ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter Y: ");
            y = int.Parse(Console.ReadLine());
            Console.Write("Is Wild: ");
            wild = bool.Parse(Console.ReadLine());
        
                minAge = age;
            if (minAge > age)
            {
                minAge = age;
            }
        }
        Console.WriteLine();

    Chaharpa.horse ob = new Chaharpa.horse();

    ob.Jump(minAge);
   
    ob.Print();

    }
}

Use one file for one class is a good practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM