Klassen-Template Syntaxfehler: Bezeichner
-
Hallo, folgender Code liefert den Fehler: Syntaxfehler: Bezeichner "mSize" in den Zeilen 7-9. Ich verstehe nicht ganz warum. Ich bin mir auch nicht ganz sicher, ob ich überhaupt ein Template brauche.
#pragma once #include "Matrix.h" #include "Vector.hpp" template<int mSize> class LinearSystem { private: Vector* mpb(mSize); Vector* order(mSize); Matrix* mpA(mSize, mSize); public: LinearSystem(Matrix A, Vector b); Vector Solve(); friend void Pivot<mSize>(Matrix* A, Vector* b, Vector* x, Vector* order, int i); };
Auch bei der "friend"-Deklaration bin ich mir nicht ganz sicher, ob sie richtig geschrieben ist.
-
Vielleicht erklärst du mal, was du da eigentlich machen willst. Die Syntaxfehler kriegt man nämlich im Zweifel am leichtesten weg, indem man die betreffenden Codezeilen löscht...
-
@H-P-Wurst sagte in Klassen-Template Syntaxfehler: Bezeichner:
Vector* mpb(mSize);
Was soll mSize denn mit dem Zeiger machen?
Fehlermeldung: Copy&Paste (in VS: aus dem Output Tab!)
-
Also ich habe ziemlich viele Fehlermeldungen. Ich habe wohl irgendwas mit dem Template falsch gemacht.
Vector ist eine eigene Klasse und mSize soll die Größe des Vektors sein. Der Konstruktor von Vektor funktioniert aber, den habe ich schon ausprobiert.
-
@H-P-Wurst sagte in Klassen-Template Syntaxfehler: Bezeichner:
Also ich habe ziemlich viele Fehlermeldungen. Ich habe wohl irgendwas mit dem Template falsch gemacht.
Vector ist eine eigene Klasse und mSize soll die Größe des Vektors sein. Der Konstruktor von Vektor funktioniert aber, den habe ich schon ausprobiert.Da ist kein Vector!
-
Hier der Code von Vector (kompliliert ohne Fehler):
#ifndef VECTORHEADERDEF #define VECTORHEADERDEF class Vector { private: double* mData; // data stored in vector int mSize; // size of vector public: Vector(const Vector& otherVector); Vector(int size); ~Vector(); int GetSize() const; double& operator[](int i); // zero-based indexing // read-only zero-based indexing double Read(int i) const; double& operator()(int i); // one-based indexing // assignment Vector& operator=(const Vector& otherVector); Vector operator+() const; // unary + Vector operator-() const; // unary - Vector operator+(const Vector& v1) const; // binary + Vector operator-(const Vector& v1) const; // binary - // scalar multiplication Vector operator*(double a) const; // p-norm method double CalculateNorm(int p=2) const; // declare length function as a friend friend int length(const Vector& v); friend std::ostream& operator<<(std::ostream& output, const Vector& u); }; // Prototype signature of length() friend function int length(const Vector& v); #endif //Code from Chapter10.tex line 20 save as Vector.hpp
#include <cmath> #include <iostream> #include <cassert> #include "Vector.hpp" // Overridden copy constructor // Allocates memory for new vector, and copies // entries of other vector into it Vector::Vector(const Vector& otherVector) { mSize = otherVector.GetSize(); mData = new double[mSize]; for (int i = 0; i < mSize; i++) { mData[i] = otherVector.mData[i]; } } // Constructor for vector of a given size // Allocates memory, and initialises entries // to zero Vector::Vector(int size) { assert(size > 0); mSize = size; mData = new double[mSize]; for (int i = 0; i < mSize; i++) { mData[i] = 0.0; } } // Overridden destructor to correctly free memory Vector::~Vector() { delete[] mData; } // Method to get the size of a vector int Vector::GetSize() const { return mSize; } // Overloading square brackets // Note that this uses `zero-based' indexing, // and a check on the validity of the index double& Vector::operator[](int i) { assert(i > -1); assert(i < mSize); return mData[i]; } // Read-only variant of [] // Note that this uses `zero-based' indexing, // and a check on the validity of the index double Vector::Read(int i) const { Vector u(*this); return u[i]; } // Overloading round brackets // Note that this uses `one-based' indexing, // and a check on the validity of the index double& Vector::operator()(int i) { return (*this)[i - 1]; } // Overloading the assignment operator Vector& Vector::operator=(const Vector& otherVector) { if (mSize != otherVector.mSize) { std::cerr << "Sizes of Vectors don't match. \n"; } for (int i = 0; i < mSize; i++) { mData[i] = otherVector.mData[i]; } return *this; } // Overloading the unary + operator Vector Vector::operator+() const { Vector v(mSize); for (int i = 0; i < mSize; i++) { v[i] = mData[i]; } return v; } // Overloading the unary - operator Vector Vector::operator-() const { Vector v(mSize); for (int i = 0; i < mSize; i++) { v[i] = -mData[i]; } return v; } // Overloading the binary + operator Vector Vector::operator+(const Vector& v1) const { if (mSize != v1.mSize) { std::cerr << "Sizes of Vectors don't match. \n"; } Vector v(mSize); for (int i = 0; i < mSize; i++) { v[i] = mData[i] + v1.mData[i]; } return v; } // Overloading the binary - operator Vector Vector::operator-(const Vector& v1) const { if (mSize != v1.mSize) { std::cerr << "Sizes of Vectors don't match. \n"; } Vector v(mSize); for (int i = 0; i < mSize; i++) { v[i] = mData[i] - v1.mData[i]; } return v; } // Overloading scalar multiplication Vector Vector::operator*(double a) const { Vector v(mSize); for (int i = 0; i < mSize; i++) { v[i] = a * mData[i]; } return v; } // Method to calculate norm (with default value p=2) // corresponding to the Euclidean norm double Vector::CalculateNorm(int p) const { double norm_val, sum = 0.0; for (int i = 0; i < mSize; i++) { sum += pow(fabs(mData[i]), p); } norm_val = pow(sum, 1.0 / ((double)(p))); return norm_val; } // MATLAB style friend to get the size of a vector int length(const Vector& v) { return v.mSize; } std::ostream& operator<<(std::ostream& output, const Vector& u) { output << "("; for (int i = 0; i < u.GetSize(); i++) { output << " " << u.Read(i); } output << " )"; return output; } //Code from Chapter10.tex line 62 save as Vector.cpp
-
@manni66 sagte in Klassen-Template Syntaxfehler: Bezeichner:
Da ist kein Vector!
-
Wie erzeugt man denn einen Zeiger einer eigenen Klasse?
-
@H-P-Wurst sagte in Klassen-Template Syntaxfehler: Bezeichner:
Wie erzeugt man denn einen Zeiger einer eigenen Klasse?
So wie jeden anderen Zeiger auch. Oder hast du schon einmal einen Zeiger auf
int
alsint *foo(123);
gesehen? Wohl kaum, denn das ist keinint
-Zeiger. ABER: Man braucht in C++ eigentlich nie direkte Zeiger. Und ganz selten indirekt. Und ganz bestimmt niemals in dem Zusammenhang, wie du es hier wünscht. Daher: Warum? Lass es einfach. Zumal du offensichtlich wesentliche Probleme mit Zeigern hast.Deine Vectorklasse hat derzeit Fehler, auf die du noch nicht gestoßen bist. Du solltest mehr testen.