when i'm trying to compile program but it says that i have an error in row when i'm to create a function pow(int base ,int exp),it says that 'pow': illegal qualified name in member declaration,here's my code:
Math.h:
#pragma once
static class Math {
public:
static int Math::pow(int base,int exp);
};
Math.cpp:
#include "Math.h"
int Math::pow(int base, int exp)
{
int result = 1;
for (int i = 0; i < exp; i++)
{
result = result * base;
}
return result;
}
Line:
static int Math::pow(int base,int exp);
Does not need the Math:: prefix since it is in the Math class declaration.
The class definition is probably intended to look like this:
class Math {
public:
static int pow(int base,int exp);
};
static before the class has no meaning and is not allowed syntax.
The member function declaration doesn't need (and isn't allowed to) have a qualified name. It belongs to the class already because it is declared in the class scope. There is no need to qualify it any further.
From what you are doing it does seem however that you really want to have a namespace, not a class:
// Header file
namespace Math {
int pow(int base,int exp);
}
// Source file
namespace Math {
int pow(int base, int exp)
{
int result = 1;
for (int i = 0; i < exp; i++)
{
result = result * base;
}
return result;
}
}
Related
Below is a piece of code I was trying to run where I have function I want to run (dN) within my main function which returns a value of type complex<double>.
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1(0.0,1.0); //imaginary number definition
class Functions{
public:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
int main(int argc, const char * argv[]) {
//...more code here
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
Although this code does not run and returns the error: Call to non-static member function without an object argument
Based on my understanding, C++ does not allow nested functions and my code above would not work because I call a separate function in my main function. Although (based on the link) it does appear one can implement "local classes" by defining a function within a struct which would have to be inside the main function. Although when I try doing that:
int main(int argc, const char * argv[]) {
complex<int> **NM = new complex<int>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<int>[500];
}
struct Functions{
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
complex<double> OUT = Im1*(N[k][i]+kN)/(T1);
return OUT;
};
};
complex<double> dN_OUT = Functions::dN(**NM,1,20,0.,20.);
return 0;
};
the error persists. Ultimately, I am simply wanting to use the function dN that returns output of type complex<double> within my main function, but am unsure of the best/operational way to implement this.
I believe you misunderstand what a nested function is.
A nested function would look like this:
int main()
{
void nested() {} // not allowed in C++
}
The solution to your problem is in the error message provided by your compiler:
Call to non-static member function without an object argument
Take a look at the following:
// Example 1
struct Functions {
void func() {}
};
int main()
{
// to call Functions::func() you would need to have an object
// of type Functions because Functions::func() is not a static function
Functions f;
f.func();
}
// Example 2
// by making func() static you can call it without an object:
struct Functions {
static void func() {}
};
int main()
{
Functions::func(); // OK
}
Ultimately, I am simply wanting to use the function dN that returns output of type complex within my main function, but am unsure of the best/operational way to implement this.
Use a free function, like main is, unless dN has a specific reason to be part of a class:
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1)
{
...
}
int main(int argc, const char * argv[]) {
...
//like this
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
//not like this
//complex<double> dN_OUT = dN(**NM,1,20,0.,20.);
}
Option 1:
You can do this with class like below
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
const complex<double> Im1 (0.0, 1.0); //imaginary number definition
class Functions {
public:
complex<double> dN (complex<double> **N, int k, int i, complex<double> kN, double T1)
{
complex<double> OUT = Im1*(N[k][i] + kN) / (T1);
return OUT;
};
};
int main (int argc, const char * argv[]) {
//...more code here
complex<double> **NM = new complex<double>*[1000]; //1000x500 array
//run loop to initialize
for (int i = 0; i < 1000; ++i)
{
NM[i] = new complex<double>[500];
}
Functions fun; //create class instance
//call the function NOTE the changes here i.e not correct passing **NM
complex<double> dN_OUT = fun.dN (NM, 1, 20, 0., 20.);
return 0;
};
Option 2 (mentioned by others direct calling with changes instead of **NM you should use NM.
complex<double> dN(complex<double> **N, int k, int i, complex<double> kN, double T1){
...
}
int main(int argc, const char * argv[]) {
...
complex<double> dN_OUT = dN(NM,1,20,0.,20.);
}
Friend function unable to access private member of the class in which it was declared
I am trying to recreate a sample program I am reading in a book demonstrating the use of a friend function. I am getting an error in the ever annoying "Intellisense" with Visual Studio which I frankly can't stand, saying that my defined friend function does not have access to the private member in which the declaration of the friend function exists. The code in different files is as follows:
Main.cpp:
#include "stdafx.h"
#include <iostream>
#include "Budget.h"
using namespace std;
int main() {
return 0;
}
Budget.h
#pragma once
#include "Auxi.h"
class Budget {
public:
Budget() {
divisionBudget = 0;
}
double getDivisionBudget() const {
return divisionBudget;
}
void addBudget(int n) {
divisionBudget += n;
corporateBudget += n;
}
double getCorporateBudget() {
return corporateBudget;
}
static void setCorporateBudget(double n) {
corporateBudget = n;
}
friend void AuxillaryOffice::addBudget(double, Budget &);
private:
static double corporateBudget;
double divisionBudget;
};
double Budget::corporateBudget = 0;
Auxi.h
#pragma once
class Budget;
class AuxillaryOffice {
public:
AuxillaryOffice() {
divisionBudget = 0;
}
void addBudget(double n, Budget &b);
private:
double divisionBudget;
};
Auxi.cpp
#include "stdafx.h"
#include "Auxi.h"
#include "Budget.h"
// class Budget; Do I need this here?
void AuxillaryOffice::addBudget(double n, Budget &b) {
divisionBudget += n;
b.corporateBudget += n; // THIS IS THE ERROR LINE
}
In the immediately above Auxi.cpp file, I am getting an error on the b.corporateBudget += n; line it says that corporateBudget is inaccessible. Is this all legal C++ code or is there something I am missing in this example?
Thanks.
Butget.h
#pragma once
#include "Auxi.h"
class Budget {
public:
Budget() {
divisionBudget = 0;
}
double getDivisionBudget() const {
return divisionBudget;
}
void addBudget(int n) {
divisionBudget += n;
corporateBudget += n;
}
double getCorporateBudget() {
return corporateBudget;
}
static void setCorporateBudget(double n) {
corporateBudget = n;
}
friend void AuxillaryOffice::addBudget(double, Budget &);
private:
static double corporateBudget;
double divisionBudget;
};
double Budget::corporateBudget = 0; // HERE ERROR!!!
You should move double Budget::corporateBudget = 0 to Auxi.cpp.
You are trying to declare a method (AuxillaryOffice::addBudget) as friend of your class (Budget). I believe that it is not possible to declare a single method as friend. I can see two solutions here:
Restructure your program that AuxillaryOffice::addBudget becomes a function (not a class method).
Make class AuxillaryOffice a friend of class Budget.
Please, consider reading this article to get more information about friends.
Intellisense uses another compiler to check your code, so it can sometimes give different results. The error it gives you here is wrong - your code works.
There is just one error that probably prevents your code from compiling. You define the static corporateBudget in the header. This is a problem as it may be defined multiple times if the header is included multiple times (like you did here).
The cleanest solution would be to create a file Budget.cpp and define it there.
There is from 3.4.1/14:
If a variable member of a namespace is defined outside of the scope of
its namespace then any name that appears in the definition of the
member (after the declarator-id) is looked up as if the definition of
the member occurred in its namespace.
If that name treated as the definition of the member name then what is it point of declaration?
And why the following example will works:
namespace N
{
extern int j;
}
int i = 2;
int N::j = i; //N::j=2
int N::j=i actual appears into the namespace scope. Hence the declarationint i=2 is not visible for unqualified name lookup. Why does this declaration found?
Your question:
int N::j=i actual appears into the namespace scope. Hence the declaration int i=2 is not visible for unqualified name lookup. Why does this declaration found?
Answer:
Since i is not found in the N namespace, it is looked up in the global namespace. Had an i been there in the N namespace, that would have been used to initialize N::j.
Hope the following program clarifies your doubt.
#include <iostream>
namespace N
{
extern int j;
extern int k;
int x = 3;
}
int x = 2;
int y = 10;
int N::j = x; // N::x is used to initialize N::j
int N::k = y; // ::y is used to initialize N::k
int main()
{
std::cout << N::j << std::endl;
std::cout << N::k << std::endl;
}
Output:
3
10
Update, in response to comment by OP
What the standard is saying is that:
namespace N
{
extern int j;
}
int x = 2;
int N::j = x;
is equivalent to:
namespace N
{
extern int j;
}
int x = 2;
namespace N
{
int j = x;
}
The logic for lookup ofx is same. If it is found within the same namespace N, it is used. If x is not found in namespace N, it is searched for outward in the enclosing namespaces.
You seem to be confused about how name lookup works on a basic level. Maybe a simple example helps:
#include <iostream>
void print(std::string const & s) { std::cout << "Boo: " << s << "\n"; }
namespace Foo
{
std::string message = "Foo";
void action() { print(message); }
}
int main() { Foo::action(); }
Clearly the name print is visible in the definition of Foo::action. Names from containing namespaces are visible in contained namespaces. There's nothing unusual about that.
The point of the rule you are quoting, and which R Sahu already demonstrated nicely, is that you can put the definition of a variable elsewhere from its declaration, and in that case any name appearing in the initializer is looked up in the namespace in which the variable is declared. Here's another example:
namespace Foo
{
namespace Bar { int a = 10; }
int b = 20;
extern int c;
}
namespace Bar { int a = -20; }
int b = 5;
int Foo::c = Bar::a + b; // uses Foo::Bar::a and Foo::b, NOT ::Bar::a or ::b
int main() { return Foo::c; } // returns 30
I don't understand pointers and references very well yet, but I have a class with static methods and variables that will be referenced from main and other classes. I have a variable defined in main() that I want to pass to a variable in this class with static functions. I want those functions to change the value of the variable that is seen in the main() scope.
This is an example of what I am trying to do, but I get compiler errors...
class foo
{
public:
static int *myPtr;
bool somfunction() {
*myPtr = 1;
return true;
}
};
int main()
{
int flag = 0;
foo::myPtr = &flag;
return 0;
}
Provide the definition of the static variable outside the class as:
//foo.h
class foo
{
public:
static int *myPtr; //its just a declaration, not a definition!
bool somfunction() {
*myPtr = 1;
//where is return statement?
}
}; //<------------- you also forgot the semicolon
/////////////////////////////////////////////////////////////////
//foo.cpp
#include "foo.h" //must include this!
int *foo::myPtr; //its a definition
Beside that, you also forgot the semicolon as indicated in the comment above, and somefunction needs to return a bool value.
#include <iostream>
using namespace std;
class foo
{
public:
static int *myPtr;
bool somfunction() {
*myPtr = 1;
return true;
}
};
//////////////////////////////////////////////////
int* foo::myPtr=new int(5); //You forgot to initialize a static data member
//////////////////////////////////////////////////
int main()
{
int flag = 0;
foo::myPtr = &flag;
return 0;
}
A point from ISO draft N3290 :
Unqualified Name Lookup :section 3.4.1, para 14:
If a variable member of a namespace is defined outside of the scope of its
namespace then any name that appears in the definition of the member
(after the declarator-id) is looked up as if the definition of the member
occurred in its namespace.
ex:
namespace N {
int i = 4;
extern int j;
}
int i = 2;
int N::j = i; // N::j == 4
Is there any other possiblitly to prove this point other than using 'extern' keyword
can u give some other examples ... other than extern
Another example which doesn't use extern keyword:
//demo.cpp
int i = 100;
namespace N
{
int i = 200;
struct A
{
static int m;
};
}
int N::A::m = i; //i is 100 or 200?
int main()
{
cout << N::A::m << endl; //what will it print?
}
Output:
200
Online demo : http://www.ideone.com/pRVAb
This code doesn't use extern yet it more or less proves the point. Note that it doesn't define variable outside the namespace, it instead defines function outside the namespace.
//demo.cpp
int i = 100;
namespace N
{
int i = 200;
struct X{};
void f(const X&);
}
void N::f(const X&)
{
cout << i << endl; //what will it print?
}
int main()
{
N::X x;
f(x); //use argument-dependent lookup (ADL) to find the function!
}
Output:
200
Online demo : http://www.ideone.com/KCqUV
I added ADL to make it more interesting!
Another example is regarding the definition of a static member in class.
// header
namespace N {
const int i = 4;
class A
{
public:
static int m_i;
};
}
// source file
int i = 2;
int N::A::m_i = i; // N::A::m_i == 4
int main(int argc, char* argv[])
{
cout << N::A::m_i << endl;
return 0;
}
// header.h
struct X { void bar () {} };
namespace N {
struct X { void bar () {} };
void foo(X *p = new X);
}
// implementation.cpp
#include"header.h"
N::foo(X* p) { p->bar(); } // N::X::bar() called
This example is without using extern. (though it's implied).