Search code examples
c#.netvisual-studioc#-3.0replit

hello please how to solve this error .error CS1525: Unexpected symbol `namespace', expecting `identifier' or `static' Compilation failed: 1 error(s)


hello please how can i solve this error( mcs -out:main.exe main.cs main.cs(8,6): error CS1525: Unexpected symbol namespace', expecting identifier' or `static' Compilation failed: 1 error(s), 0 warnings ) from this exercice ( use the + operation to add two times together Write a program to test a class)

    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);
      }
      }
    }

Solution

  • You do not have to use using before namespace :)

    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);
        }
      }
    }
    

    Looks like the copyFrom methods are not found, hope you can figure out and fix