-
Odd error
I feel as if I have correctly setup my pointer and addresses within te arguments, but that's more than likely where I'm going wrong.
Quick question: Do you need the text object to be a pointer in the prototype of fadeMessage also?
Code:
#include <SFML/Graphics.hpp>
#include <time.h>
#include <string>
using namespace std;
void fadeMessage(sf::Text *text, unsigned int length);
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 450), "Tpyor");
window.setFramerateLimit(60);
sf::Text text("Welcome to my game.", sf::Font::getDefaultFont(), 36);
fadeMessage(sf::Text &text, 200);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(text);
window.display();
}
return 0;
}
//text object, effect length (max 255)
void fadeMessage(sf::Text *text, unsigned int length){
*text.setString("worked");
}
Error:
Code:
C:\Users\Justin\Desktop\Logic\main.cpp||In function 'int main()':|
C:\Users\Justin\Desktop\Logic\main.cpp|15|error: expected primary-expression before '&' token|
C:\Users\Justin\Desktop\Logic\main.cpp||In function 'void fadeMessage(sf::Text*, unsigned int)':|
C:\Users\Justin\Desktop\Logic\main.cpp|34|error: request for member 'setString' in 'text', which is of non-class type 'sf::Text*'|
||=== Build finished: 2 errors, 0 warnings ===|
-
fadeMessage(sf::Text &text, 200);
&text would have been fine. This looks like a reference declaration, which is not possible in this context and confused the compiler.
*text.setString("worked");
Use the arrow operator:
text->setString("worked"); equivalent to (*text).setString("worked");
Yes, the parens matter.
-
Thank you! Simple mistakes on my part. ^.^
Revised and working code:
Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
using namespace std;
void fadeMessage(sf::Text *text, int length);
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 450), "Tpyor");
window.setFramerateLimit(60);
sf::Text text("Welcome to my game.", sf::Font::getDefaultFont(), 36);
fadeMessage(&text, 200);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(text);
window.display();
}
return 0;
}
//text object, effect length (max 255)
void fadeMessage(sf::Text *text, int length){
while(length > 0){
length--;
cout << "text->setColor(sf::Color(255,255,255,(" << length << ")));" << endl;
text->setColor(sf::Color(255,255,255,length));
}
}