Avatar billede sf Nybegynder
28. august 2004 - 01:25 Der er 13 kommentarer og
1 løsning

OOP begynder spm

har disse 2 filer

==== count.h ====
template<class Count_Type>
class Count {
public:
    Count() { }
    virtual ~Count() { }
   
    virtual void increment () = 0;
    virtual void decrement () = 0;
    void reset() {value = reset_value; }

    Count_Type get_value() { return value; }
    void set_value(Count_Type new_value) { value = new_value; }
   
protected:

    Count_Type value;
    Count_Type reset_value;

};


class Integer_Count : public Count<int>{
public:

  Integer_Count() { reset_value = 0; reset(); }
  Integer_Count(int new_value) { reset_value = 0; value = new_value; }
  ~Integer_Count() { }
 
  void increment();
  void decrement();
  char* asBase(int number_base);

};


void Integer_Count::increment();
{
  value++;
}

void Integer_Count::decrement();
{
  value--;
}

char* Integer_Count::asBase(int new_base)
{
  char answer_string[80];
  char partial_answer_string[80];
  char remainder_string[10];
  int quotient;
  int remainder;

  remainder = value % new_base;
  quotient = value / new_base;
  itoa(remainder, remainder_string, new_base);
  strcpy(answer_string, remainder_string);
  while(quotient !=0)
    {
      remainder = quotient % new_base;
      quotient = value / new_base;
      itoa(remainder, remainder_string, new_base);
      strcpy(partial_answer_string, remainder_string);
      strcat(partial_answer_string, answer_string);
      strcpy(answer_string, partial_answer_string)
     
     
    }

  return answer_string;
}


og

==== OOPtest1.c ====
#include <iostream.h>
#include "count.h"

int main ()
{

Integer_Count intcount(5);
 
cout << endl << "Inteer Demo" << endl <<endl;
cout << "Current value:  " << intcount.get_value() << endl;

intcount.increment();
intcount.increment();


cout << "Current value:  " << intcount.get_value() << endl;

intcount.decrement();
intcount.decrement();
intcount.decrement();

cout << "Current value:  " << intcount.get_value() << endl;

intcount.reset();

cout << "Current value:  " << intcount.get_value() << endl;

cout << endl << "The end" << endl;

return 0;

}


men min compiler kommer med disse fejl og kan ikke finde ud af hvad der er galt

In file included from OOPtest1.c:1:
OOPtest1.h:33: error: `void Integer_Count::increment()' and `void Integer_Count::increment()' cannot be overloaded
OOPtest1.h:34: error: expected unqualified-id before '{' token
OOPtest1.h:38: error: `void Integer_Count::decrement()' and `void Integer_Count::decrement()' cannot be overloaded
OOPtest1.h:39: error: expected unqualified-id before '{' token
OOPtest1.h:44: error: `char* Integer_Count::asBase(int)' and `char* Integer_Count::asBase(int)' cannot be overloaded
OOPtest1.h: In member function `char* Integer_Count::asBase(int)':
OOPtest1.h:53: error: `itoa' undeclared (first use this function)
OOPtest1.h:53: error: (Each undeclared identifier is reported only once for each function it appears in.)
OOPtest1.h:54: error: `strcpy' undeclared (first use this function)
OOPtest1.h:61: error: `strcat' undeclared (first use this function)
OOPtest1.h:65: error: expected `;' before '}' token
OOPtest1.h:45: warning: address of local variable `answer_string' returned
In file included from /usr/include/c++/3.4/backward/iostream.h:31,
                from OOPtest1.c:2:
/usr/include/c++/3.4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
In file included from /usr/include/c++/3.4/cstring:51,
                from /usr/include/c++/3.4/i486-linux/bits/c++locale.h:41,
                from /usr/include/c++/3.4/iosfwd:46,
                from /usr/include/c++/3.4/ios:44,
                from /usr/include/c++/3.4/ostream:45,
                from /usr/include/c++/3.4/iostream:45,
                from /usr/include/c++/3.4/backward/iostream.h:32,
                from OOPtest1.c:2:
/usr/include/string.h: At global scope:
/usr/include/string.h:83: error: `char* strcpy(char*, const char*)' used prior to declaration
/usr/include/string.h:90: error: `char* strcat(char*, const char*)' used prior to declaration
Avatar billede bertelbrander Novice
28. august 2004 - 01:35 #1
Et par små rettelser så kompilerer og kører det. Jeg har lavet det som én fil:

#include <stdlib.h>
#include <string.h>
#include <iostream.h>

template<class Count_Type>
class Count {
public:
    Count() { }
    virtual ~Count() { }

    virtual void increment () = 0;
    virtual void decrement () = 0;
    void reset() {value = reset_value; }

    Count_Type get_value() { return value; }
    void set_value(Count_Type new_value) { value = new_value; }

protected:

    Count_Type value;
    Count_Type reset_value;

};


class Integer_Count : public Count<int>
{
public:

  Integer_Count() { reset_value = 0; reset(); }
  Integer_Count(int new_value) { reset_value = 0; value = new_value; }
  ~Integer_Count() { }

  void increment();
  void decrement();
  char* asBase(int number_base);

};


void Integer_Count::increment()
{
  value++;
}

void Integer_Count::decrement()
{
  value--;
}

char* Integer_Count::asBase(int new_base)
{
  static char answer_string[80];
  char partial_answer_string[80];
  char remainder_string[10];
  int quotient;
  int remainder;

  remainder = value % new_base;
  quotient = value / new_base;
  itoa(remainder, remainder_string, new_base);
  strcpy(answer_string, remainder_string);
  while(quotient !=0)
    {
      remainder = quotient % new_base;
      quotient = value / new_base;
      itoa(remainder, remainder_string, new_base);
      strcpy(partial_answer_string, remainder_string);
      strcat(partial_answer_string, answer_string);
      strcpy(answer_string, partial_answer_string);


    }

  return answer_string;
}

int main ()
{

  Integer_Count intcount(5);

  cout << endl << "Inteer Demo" << endl <<endl;
  cout << "Current value:  " << intcount.get_value() << endl;

  intcount.increment();
  intcount.increment();


  cout << "Current value:  " << intcount.get_value() << endl;

  intcount.decrement();
  intcount.decrement();
  intcount.decrement();

  cout << "Current value:  " << intcount.get_value() << endl;

  intcount.reset();

  cout << "Current value:  " << intcount.get_value() << endl;

  cout << endl << "The end" << endl;

  return 0;

}
Avatar billede bertelbrander Novice
28. august 2004 - 01:37 #2
De vigtigste ændringer er

#include <stdlib.h>
#include <string.h>

og fjerne ; efter
void Integer_Count::decrement()
void Integer_Count::increment()

asBase() skal nok fixes.
Avatar billede sf Nybegynder
28. august 2004 - 02:02 #3
kommer stadig med lidt fejl .. der skulle ikke v;re noget problem med at compile med g++ 3.4 ?
Avatar billede bertelbrander Novice
28. august 2004 - 02:04 #4
Min g++ (mingw) 3.3.1 har ikke problemer.
Hvilke fejl får du?
Avatar billede bertelbrander Novice
28. august 2004 - 02:06 #5
Du bør udskifte:

#include <iostream.h>

med

#include <iostream>
using namespace std;
Avatar billede sf Nybegynder
28. august 2004 - 02:09 #6
faa disse fejl:

g++-3.4 hjelptiloop.c
hjelptiloop.c: In member function `char* Integer_Count::asBase(int)':
hjelptiloop.c:62: error: `itoa' undeclared (first use this function)
hjelptiloop.c:62: error: (Each undeclared identifier is reported only once for each function it appears in.)
Avatar billede sf Nybegynder
28. august 2004 - 02:11 #7
er faktisk det sidste fejl saa det hjaelper da paa det..
Avatar billede bertelbrander Novice
28. august 2004 - 02:14 #8
itoa bør være defineret i stdlib.h
Men det er ikke en Ansi C/C++ funktion, så den er der måske ikke.

asBase bliver vist ikke brugt, så prøv at udkommenter den.
Avatar billede bertelbrander Novice
28. august 2004 - 02:14 #9
Der manglede også en ; i asBase
Avatar billede sf Nybegynder
28. august 2004 - 02:16 #10
ja har jeg ogsaa gjordt og nu virker det .. tak lige en ting lade til du har styr paa det her.. :) hvorfor:

#include <iostream>
using namespace std;

du kan bare lave et svar
Avatar billede bertelbrander Novice
28. august 2004 - 02:16 #11
Du kan måske erstatte atoi med en sprintf.
Avatar billede bertelbrander Novice
28. august 2004 - 02:19 #12
iostream.h er en pre AnsiC++ headerfil.
Den er blevet erstattet af iostream hvor alt er puttet ind i et namespace kaldet std

Min GCC giver følgende warning hvis jeg inkluderer iostream.h:

G:/Win32App/mingw/include/c++/3.3.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.

Jeg forsøger at undgå point.
Avatar billede sf Nybegynder
28. august 2004 - 02:30 #13
hvorfor??
Avatar billede sf Nybegynder
09. november 2004 - 23:00 #14
tager jeg selv point selv ellers må du sige til
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester





White paper
Tidsbegrænset kampagne: Overvejer du at udskifte eller tilføje printere i din forretning? Vi kan tilbyde én eller flere maskiner gratis