0

I'm making a library for myself, and I keep getting that error :(
Pib.h:

#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Pib{
public:
	void echo (int Text);
	void test(void);
};
void Pib::echo(int Text){
	printf("Haha: "+Text);
};
void Pib::test(void){
	cout<<"Text";
};

Main File:

#include "stdafx.h"
#include "Pib.h"
#include <iostream>
#include <string>
int main(int argc, _TCHAR* argv[]){
	string a="D:";
	Pib::test();
	getline(cin,a);
	return 0;
}

Thanks if you can help! Keep in mind that I am learning C++ and may need some extra help putting in fixes, :P

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by firstPerson
Featured Replies
  • I got it to print "Text" to the screen using this code: [CODE]#pragma once #include <iostream> #include <string> using namespace std; class Pib{ public: void echo (int Text); void test(void); }; void Pib::echo(int Text){ printf("Haha: "+Text); }; void Pib::test(void){ cout<<"Text"; };[/CODE] [CODE]#include "Pib.h" #include <iostream> #include <string> int main(){ string … Read More

  • I don't believe you can call class functions without an object. Namespaces use the same :: operator as classes and don't use objects. [CODE]namespace Pib { void test(); } void Pib::test() { cout << "Test" << endl; } [/CODE] in main() [CODE]Pib::test();[/CODE] See this link for all about namespaces [URL="http://msdn.microsoft.com/en-us/library/014batd5%28VS.80%29.aspx"]http://msdn.microsoft.com/en-us/library/014batd5%28VS.80%29.aspx[/URL] Read More

1

I got it to print "Text" to the screen using this code:

#pragma once
#include <iostream>
#include <string>
using namespace std;

class Pib{
public:
	void echo (int Text);
	void test(void);
};
void Pib::echo(int Text){
	printf("Haha: "+Text);
};
void Pib::test(void){
	cout<<"Text";
};
#include "Pib.h"
#include <iostream>
#include <string>

int main(){
    string a="D:";
    Pib pibtest;
    pibtest.test();
    getline(cin,a);
    
    return 0;
}

I was using MS VS 2008 Express Edition.

Comments
Thanks alot!
0

When you use a class that you've created you need to make an object of that type in you're main function.

Pib myPib

then test can be called with

myPib.test()
0

I read online you could call functions like Pib::test();
Know how I could do this? If I have to use this method, I will, i'm just curious. Also - Can I make a inner-object?
ALSO: Thanks for quick replys!

0

I did some modifying and made it use namespace. I wanted this because it had intelisense support. Thanks A LOT you two! +Rep!

Edited by Someguynamedpie: n/a

0

>>I don't believe you can call class functions without an object.
It can be done.

#include <iostream>
#include <string>

using namespace std;

struct Print
{
	static void print(string str)
	{
		cout << str << endl;
	}
};

int main(){	
	Print::print("string");
}

But from the looks of it, using a namespace would be a better idea.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.