【问题标题】:hello please how to solve this error .error CS1525: Unexpected symbol `namespace', expecting `identifier' or `static' Compilation failed: 1 error(s) [closed]你好请问如何解决这个错误。错误CS1525:意外符号“命名空间”,期待“标识符”或“静态”编译失败:1个错误[关闭]
【发布时间】:2021-03-15 13:09:15
【问题描述】:

你好请问我该如何解决这个错误(mcs -out:main.exe main.cs main.cs(8,6): error CS1525: Unexpected symbol namespace', expecting identifier' or `static' 编译失败:1 个错误,0 个警告 )从这个练习(使用+操作将两次相加编写一个程序来测试一个类)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using namespace v3
    {
          public class Time
        {
          private int hr, mn, sc;
            public Time()
            {
                hr = 0;
                mn = 0;
                sc = 0;
            }
            public Time(int h, int m, int s)
            {
                hr = h;
                mn = m;
                sc = s;
            }
        
      public void display()
      {
        Console.WriteLine(hr);
        Console.WriteLine(":");
        Console.WriteLine(mn);
        Console.WriteLine(":");
        Console.WriteLine(sc);
        Console.WriteLine("\n");
      }
      public static class MembresGlobals
    {
        static int Main()
      {
      Time time_1 = new Time(3, 5, 2);
      Time time_2 = new Time(2, 5, 3);
      Time time_sum = new Time();

      time_sum.CopyFrom(time_1 + time_2);
      time_1.display();
      time_2.display();
      time_sum.display();
      }

    }
      public static Time operator + (Time ImpliedObject, Time t2)
      {
      int totalsecs = (ImpliedObject.hr * 3600) + (ImpliedObject.mn * 60) + ImpliedObject.sc + (t2.hr * 3600) + (t2.mn * 60) + t2.sc;
      int h = totalsecs / (60 * 60);
      int m = totalsecs % (60 * 60) / 60;
      int s = totalsecs % (60 * 60) % 60;
      return new Time(h, m, s);
      }
      }
    }

【问题讨论】:

  • “你不必在命名空间之前使用 using” - 来自 Saravanan 的回答

标签: c# .net visual-studio c#-3.0 repl.it


【解决方案1】:

你不必在命名空间之前使用 using :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace v3 {
  public class Time {
    private int hr,
    mn,
    sc;
    public Time() {
      hr = 0;
      mn = 0;
      sc = 0;
    }
    public Time(int h, int m, int s) {
      hr = h;
      mn = m;
      sc = s;
    }

    public void display() {
      Console.WriteLine(hr);
      Console.WriteLine(":");
      Console.WriteLine(mn);
      Console.WriteLine(":");
      Console.WriteLine(sc);
      Console.WriteLine("\n");
    }
    public static class MembresGlobals {
      static int Main() {
        Time time_1 = new Time(3, 5, 2);
        Time time_2 = new Time(2, 5, 3);
        Time time_sum = new Time();

        time_sum.CopyFrom(time_1 + time_2);
        time_1.display();
        time_2.display();
        time_sum.display();
      }

    }
    public static Time operator + (Time ImpliedObject, Time t2) {
      int totalsecs = (ImpliedObject.hr * 3600) + (ImpliedObject.mn * 60) + ImpliedObject.sc + (t2.hr * 3600) + (t2.mn * 60) + t2.sc;
      int h = totalsecs / (60 * 60);
      int m = totalsecs % (60 * 60) / 60;
      int s = totalsecs % (60 * 60) % 60;
      return new Time(h, m, s);
    }
  }
}

好像没有找到copyFrom方法,希望你能解决

【讨论】:

  • 这听起来应该作为错字关闭。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多