Error cs9010 primary constructor body is not allowed

Ошибка : error CS9010: Primary Constructor body is not allowed

Пытаюсь сделать управление и ошибка которую я не понимаю как фиксить

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
{
public float speed;

private rigidbody2D rb;
private Vector2 moveInput;
private Vector2 moveVelocity;

void Start ()
{
rb = GetComponent<rigidbody2D> ();
}

void Update ()
{
moveInput = new Vector2 (Input.GetAxisRaw («Horizontal»), Input.GetAxisRaw («Vertical»));
moveVelocity = moveInput.normalized * speed;
}

voidFixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
}
}

Ошибка : error CS9010: Primary Constructor body is not allowed

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

A. D.

I’m getting this compilation error in the console, but as far as I can tell I’ve followed the steps closely. I can’t see any error in spelling or syntax. I’ve got the Point.cs and the Map.cs scripted correctly, I believe.


using System;

namespace TreehouseDefense
{
class Game
{
public static void Main()
{
//Tower tower = new Tower();

      Map map = new Map(8, 5);

      Point point = new Point(4, 2);
      bool isOnMap = map.OnMap(point);
      Console.WriteLine(isOnMap);

      point = new Point(8, 5);
      isOnMap = map.OnMap(point);
      Console.WriteLine(isOnMap);
    }
}

}


3 Answers

Steven Parker

You have a stray semicolon, possibly just a typo.

It was in another module! Now that I can test the build, I see the full error message which is:

Point.cs(11,5): error CS9010: Primary constructor body is not allowed  

The beginning of the line tells you that the error is in the module Point.cs at line 11 and column 5. That would be the brace a the beginning of the constructor body. But while that’s the point at which a syntax error was detected, the actual cause of the problem is the stray semicolon at the end of the previous line (line 10).

A. D.

A. D.

April 10, 2017 9:59pm

A. D.

A. D.

April 10, 2017 11:19pm

Geez, foiled by ye ol’ semicolon / colon switch-e-roo! Thanks, for catching that.

Несколько проблем с вашим приложением:

  • Реализации метода не принимают ; в конце.

  • Console.ReadLine не принимает никаких аргументов. Его единственная цель — прочитать консоль, поэтому вы должны использовать Console.WriteLine чтобы задать свои вопросы, например «Введите число».

  • Вы должны перенести свой класс в пространство имен (которое обычно называется вашим проектом.csproj)

И, пожалуйста, следуйте соглашениям С#, с именами классов как PascalCase и переменными как camelCase. Когда вы делитесь своим кодом, другим разработчикам проще читать и понимать его.

Поэтому он должен выглядеть так:

using System;

namespace MyApp
{
    public class LargestNumber
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int userNumber1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter a second number:");
            int userNumber2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter a third number:");
            int userNumber3 = int.Parse(Console.ReadLine());

            Console.WriteLine("Your numbers were, " + userNumber1 + ", " + userNumber2 + ", and " + userNumber3);
        }
    }
}

Пожалуйста, обратите внимание, что я удалил некоторые из ваших бесполезных с using утверждений.

Вы также должны переименовать свой файл Number.cs в LargestNumber.cs, вы обычно хотите, чтобы ваш файл имел то же имя, что и ваш класс.


Кроме того, я не получаю ту же ошибку компиляции, что и вы, даже путем копирования кода.

В VS2015 у меня есть «Non-abstract и non-extern метод должен объявить тело» (вызванный дополнительной точкой с запятой), а «Метод ReadLine имеет 0 параметров, но вызывается с 1 аргументом». Ошибка, которую вы получаете, может быть вызвана синтаксическими ошибками в другом месте вашего кода.

Hey, I’m running into some issues compiling this using mcs on MacOS High Sierra. Specifically, here’s my terminal output after running
mcs -pkg:dotnet /reference:System.Drawing.dll *.cs and installing mono using brew. I’m not at all familiar with C# or .NET, so any guidance with this would be appreciated … it definitely looks like a versioning issue.

Here’s my mono version:

Mono JIT compiler version 5.10.1.47 (2017-12/8eb8f7d5e74 Fri Apr 13 20:18:12 EDT 2018)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           normal
	SIGSEGV:       altstack
	Notification:  kqueue
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	Interpreter:   yes
	LLVM:          yes(3.6.0svn-mono-master/8b1520c8aae)
	GC:            sgen (concurrent by default)

And here’s my output:

OverlappingModel.cs(50,17): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
OverlappingModel.cs(50,28): error CS1525: Unexpected symbol `int', expecting `,', `;', or `='
OverlappingModel.cs(50,33): error CS1525: Unexpected symbol `byte', expecting `,', `;', or `='
OverlappingModel.cs(55,4): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(70,4): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(92,4): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(97,5): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
OverlappingModel.cs(97,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(97,21): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
OverlappingModel.cs(97,44): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
OverlappingModel.cs(97,50): error CS1519: Unexpected symbol `-' in class, struct, or interface member declaration
OverlappingModel.cs(97,54): error CS1519: Unexpected symbol `+' in class, struct, or interface member declaration
OverlappingModel.cs(97,62): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
OverlappingModel.cs(97,76): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(97,83): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
OverlappingModel.cs(97,106): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
OverlappingModel.cs(97,112): error CS1519: Unexpected symbol `-' in class, struct, or interface member declaration
OverlappingModel.cs(97,116): error CS1519: Unexpected symbol `+' in class, struct, or interface member declaration
OverlappingModel.cs(97,124): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
OverlappingModel.cs(98,4): error CS9010: Primary constructor body is not allowed
OverlappingModel.cs(122,5): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(122,20): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(123,15): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(123,25): error CS1519: Unexpected symbol `+' in class, struct, or interface member declaration
OverlappingModel.cs(123,28): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
OverlappingModel.cs(123,33): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(124,12): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(124,23): error CS1519: Unexpected symbol `T' in class, struct, or interface member declaration
OverlappingModel.cs(124,24): error CS1519: Unexpected symbol `]' in class, struct, or interface member declaration
OverlappingModel.cs(124,25): error CS1525: Unexpected symbol `]', expecting `,' or `]'
OverlappingModel.cs(125,16): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(125,29): error CS1519: Unexpected symbol `T' in class, struct, or interface member declaration
OverlappingModel.cs(125,30): error CS1519: Unexpected symbol `]' in class, struct, or interface member declaration
OverlappingModel.cs(128,9): error CS1519: Unexpected symbol `foreach' in class, struct, or interface member declaration
OverlappingModel.cs(128,20): error CS1519: Unexpected symbol `in' in class, struct, or interface member declaration
OverlappingModel.cs(128,30): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
OverlappingModel.cs(129,3): error CS9010: Primary constructor body is not allowed
OverlappingModel.cs(129,3): error CS8041: Primary constructor already has a body
OverlappingModel.cs(140,4): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
OverlappingModel.cs(142,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(142,24): error CS1519: Unexpected symbol `4' in class, struct, or interface member declaration
OverlappingModel.cs(142,26): error CS1525: Unexpected symbol `]', expecting `,' or `]'
OverlappingModel.cs(142,28): error CS1525: Unexpected symbol `]', expecting `,' or `]'
OverlappingModel.cs(143,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
OverlappingModel.cs(143,21): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
OverlappingModel.cs(143,28): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
OverlappingModel.cs(144,3): error CS9010: Primary constructor body is not allowed
OverlappingModel.cs(144,3): error CS8041: Primary constructor already has a body
OverlappingModel.cs(156,20): error CS1525: Unexpected symbol `bool', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
SimpleTiledModel.cs(40,15): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='
SimpleTiledModel.cs(40,26): error CS1525: Unexpected symbol `int', expecting `,', `;', or `='
SimpleTiledModel.cs(40,36): error CS1525: Unexpected symbol `>', expecting `,', `;', or `='
SimpleTiledModel.cs(45,4): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
SimpleTiledModel.cs(49,9): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(49,28): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
SimpleTiledModel.cs(49,29): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
SimpleTiledModel.cs(50,13): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(50,31): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
SimpleTiledModel.cs(50,32): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
SimpleTiledModel.cs(56,9): error CS1519: Unexpected symbol `foreach' in class, struct, or interface member declaration
SimpleTiledModel.cs(56,28): error CS1519: Unexpected symbol `in' in class, struct, or interface member declaration
SimpleTiledModel.cs(56,43): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
SimpleTiledModel.cs(56,50): error CS1519: Unexpected symbol `tiles' in class, struct, or interface member declaration
SimpleTiledModel.cs(56,63): error CS1525: Unexpected symbol `tile'
SimpleTiledModel.cs(56,53): error CS1520: Class, struct, or interface method must have a return type
SimpleTiledModel.cs(56,68): error CS1525: Unexpected symbol `)'
SimpleTiledModel.cs(143,5): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(143,19): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
SimpleTiledModel.cs(144,11): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(144,35): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
SimpleTiledModel.cs(144,36): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
SimpleTiledModel.cs(146,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(146,24): error CS1519: Unexpected symbol `4' in class, struct, or interface member declaration
SimpleTiledModel.cs(146,26): error CS1525: Unexpected symbol `]', expecting `,' or `]'
SimpleTiledModel.cs(146,28): error CS1525: Unexpected symbol `]', expecting `,' or `]'
SimpleTiledModel.cs(148,5): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
SimpleTiledModel.cs(148,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(148,21): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(148,28): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(149,3): error CS9010: Primary constructor body is not allowed
SimpleTiledModel.cs(155,9): error CS1519: Unexpected symbol `foreach' in class, struct, or interface member declaration
SimpleTiledModel.cs(155,32): error CS1519: Unexpected symbol `in' in class, struct, or interface member declaration
SimpleTiledModel.cs(155,47): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration
SimpleTiledModel.cs(155,58): error CS1519: Unexpected symbol `neighbors' in class, struct, or interface member declaration
SimpleTiledModel.cs(155,71): error CS1525: Unexpected symbol `neighbor'
SimpleTiledModel.cs(155,61): error CS1520: Class, struct, or interface method must have a return type
SimpleTiledModel.cs(155,80): error CS1525: Unexpected symbol `)'
SimpleTiledModel.cs(176,5): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,15): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,23): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,26): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,31): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,46): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,54): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,57): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
SimpleTiledModel.cs(176,62): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(177,4): error CS9010: Primary constructor body is not allowed
SimpleTiledModel.cs(177,4): error CS8041: Primary constructor already has a body
SimpleTiledModel.cs(183,5): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
SimpleTiledModel.cs(183,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(183,21): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(183,28): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(184,3): error CS9010: Primary constructor body is not allowed
SimpleTiledModel.cs(184,3): error CS8041: Primary constructor already has a body
SimpleTiledModel.cs(189,5): error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,14): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,21): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,28): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,43): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,51): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,54): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration
SimpleTiledModel.cs(189,59): error CS1519: Unexpected symbol `++' in class, struct, or interface member declaration
SimpleTiledModel.cs(190,4): error CS9010: Primary constructor body is not allowed
SimpleTiledModel.cs(190,4): error CS8041: Primary constructor already has a body
SimpleTiledModel.cs(202,20): error CS1525: Unexpected symbol `bool', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
Compilation failed: 115 error(s), 0 warnings

Понравилась статья? Поделить с друзьями:
  • Error cs8802 только одна единица компиляции может содержать инструкции верхнего уровня
  • Error cs8641 else не может запускать оператор
  • Error cs8641 else cannot start a statement
  • Error cs5001 program does not contain a static main method suitable for an entry point
  • Error cs2001 source file could not be found